如何保存 lambda 以供以后回调?
如何修复下面的代码以存储 lambda 以便稍后调用它?
我当前收到的错误是字段“m_callback”的类型不完整。
class Foo
{
public:
Foo() { }
~Foo() { }
template< typename FuncT >
void setCallback( FuncT&& callback )
{
m_callback = callback;
}
private:
auto m_callback; // this line is broken
};
int main(int argc, char** argv)
{
Foo foo;
foo.setCallback( [] (int x){ return true; } );
return 0;
}
How do I fix the code below to store a lambda so I can invoke it at a later time?
The error I currently get is field 'm_callback' has incomplete type.
class Foo
{
public:
Foo() { }
~Foo() { }
template< typename FuncT >
void setCallback( FuncT&& callback )
{
m_callback = callback;
}
private:
auto m_callback; // this line is broken
};
int main(int argc, char** argv)
{
Foo foo;
foo.setCallback( [] (int x){ return true; } );
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
auto
关键字不能这样使用。我建议使用这样的东西:这是从 Visual Studio 2010 完成的。
The
auto
keyword can't be used liked that. I recommend using something like this:This is done from Visual Studio 2010.
auto
关键字只能与初始化表达式结合使用。所以...这有效:
...但这不行:
我建议您使用类似 的内容
function
具有特定的签名来表示回调。The
auto
keyword can only be used in conjunction with an initalization expression.So... this works:
... but this doesn't:
I would recommend that you use something like
function
with a specific signature to represent a callback.