使用std::function,我们可以使用得到一个参数的类型argument_type,second_argument_type等等类型定义,但我看不到的方式做同样的事情lambda表达式.可能吗?(我正在使用VS2010)
假设我想要在我的反序列化系统中使用以下内容来读取对象并将其传递给setter函数:
template<typename F>
static void forward(F f)
{
// Create an object of the type of the first
// parameter to the function object F
typedef typename F::argument_type T;
T t;
//...do something with 't' here (deserialize in my case)
// Forward the object to the function
f(t);
}
Run Code Online (Sandbox Code Playgroud)
它可以像这样使用,一切正常:
std::function<void(int)> f = [](int i) -> void { setValue(i); };
forward(f);
Run Code Online (Sandbox Code Playgroud)
但它不能直接与lambdas一起使用:
forward([](int i) -> void { setValue(i); });
//error C2039: 'argument_type' : …Run Code Online (Sandbox Code Playgroud)