请考虑以下一组示例。是否有可能在lambda中捕获可变数量的参数?
- 函数
takeOnlyVoidFunction
接受一个带有零参数的函数并简单地执行它。 - 函数
takeVariableArguments
需要可变数量的参数并使用参数执行该函数。 - 函数
captureVariableArgs
尝试将第二个函数转换为第一个函数可接受的lambda表单,但它不能编译。
我怎样才能使函数captureVariableArgs
编译,并表现出转换函数的参数个数可变成闭合不带参数的正确的行为?
#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);
}
我跑gcc 4.9.2
。这是我看到的编译器错误。
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...)>::
^
更新:更小例子演示了这个问题。
#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);
}
你的代码编译和工作,你看到什么问题? – ixSci
带有注释的函数不会为我编译。你在叮当还是gcc? – merlin2011
将在几分钟内更新并显示编译错误。 – merlin2011