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;
}
The auto keyword can't be used liked that. I recommend using something like this:
#include <functional>
std::function<bool (int)> m_callback;
This is done from Visual Studio 2010.
The auto keyword can only be used in conjunction with an initalization expression.
So... this works:
auto callback = [](int x){ return x == 0; };
... but this doesn't:
auto callback;
callback = [](int x){ return x == 0; };
I would recommend that you use something like function with a specific signature to represent a callback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With