Is it possible to capture a variable number of par

2019-06-18 18:55发布

问题:

Consider the following set of examples.

  1. The function takeOnlyVoidFunction takes a function with zero arguments and simply executes it.
  2. The function takeVariableArguments takes a variable number of arguments and executes the function using the arguments.
  3. The function captureVariableArgs attempts to convert the second function into a lambda form that is acceptable by the first function, but it does not compile.

How can I make the function captureVariableArgs compile and exhibit the correct behavior of converting a function with a variable number of arguments into a closure with no arguments?

#include <stdio.h>
#include <functional>

void takeOnlyVoidFunction(std::function<void()> task) {
    task();
}

template<typename _Callable, typename... _Args>
    void takeVariableArguments(_Callable&& __f, _Args&&... __args) {
     __f(__args...);
}

// How can I make this function compile?
template<typename _Callable, typename... _Args>
    void captureVariableArgs(_Callable&& __f, _Args&&... __args) {
    takeOnlyVoidFunction([=]() { __f(__args...);});
}

void normalFunction(int a, int b) {
    printf("I am a normal function which takes params (%d,%d)\n", a, b);
}

int main() {
    int a = 7;
    int b = 8;
    takeVariableArguments(normalFunction, a, b);
    takeOnlyVoidFunction([=](){ normalFunction(a,b);});
    captureVariableArgs(normalFunction, a, b);
}

I'm running gcc 4.9.2. Here is the compiler error I see.

g++ -std=c++11    Test.cc   -o Test
Test.cc: In instantiation of ‘captureVariableArgs(_Callable&&, _Args&& ...)::<lambda()> [with _Callable = void (&)(int, int); _Args = {int&, int&}]’:
Test.cc:16:38:   required from ‘struct captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’
Test.cc:16:50:   required from ‘void captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]’
Test.cc:28:45:   required from here
Test.cc:16:34: error: variable ‘__f’ has function type
     takeOnlyVoidFunction([=]() { __f(__args...);});
                                  ^
Test.cc:16:34: error: variable ‘__f’ has function type
Test.cc: In instantiation of ‘struct captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’:
Test.cc:16:50:   required from ‘void captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]’
Test.cc:28:45:   required from here
Test.cc:16:34: error: field ‘captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>::<__f capture>’ invalidly declared function type
In file included from Test.cc:2:0:
/usr/include/c++/4.9/functional:2418:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {}]’, declared using local type ‘captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’, is used but never defined [-fpermissive]
       function<_Res(_ArgTypes...)>::
       ^

Update: A more minimal example demonstrating this problem.

#include <stdio.h>

// How can I make this function compile?
template<typename _Callable>
void captureVariableArgs(_Callable&& __f) {
    takeOnlyVoidFunction( [=]{ __f(); } );
}

void normalFunction() {
    printf("I am a normal function\n");
}

int main(){
    captureVariableArgs(normalFunction);
}

回答1:

As another potential workaround for GCC, instead of using a lambda, you could use std::bind:

template <typename F, typename... Args>
auto captureVariable(F&& f, Args&&... args)
{
    return std::bind(std::forward<F>(f), std::forward<Args>(args)...);
}

This works for me under GCC 4.9.3.



回答2:

The code in the post compiles fine with the latest clang&MSVC compilers but all the gccs refuse to compile it. So it seems a bug in gcc. Nevertheless, I found a way to make gcc happy: just don't use an "universal reference" on the callable argument, like this:

template<typename _Callable, typename... _Args>
int captureVariableArgs(_Callable _f, _Args&&... _args) {
    return takeOnlyVoidFunction([=]() { _f(_args...);});
}

I can't explain why gcc doesn't accept your version, though. I'm not familiar with gcc-style error reporting and can't extract the true cause from the error message. But I think the workaround is ok since I don't see any value in "universal reference" in this case. In fact, I don't see why you use it on the args either.



标签: c++ gcc lambda