Foo works as expected. The "Bar" function object does not. For starters, why can I pass a lambda as an argument to Foo, but not to Bar?
Also, how can I achieve the construction of a bar like I'm trying to do in main?
Code:
template <class _Fxn>
void Foo(_Fxn fxn)
{
fxn();
}
template <class _Fxn>
class Bar
{
_Fxn fxn;
public:
Bar(_Fxn f) : fxn(f) {}
void operator()(){ fxn(); }
};
int main()
{
Foo([]()->void{ std::cout << "Hello World!\n"; });
Bar<void()> bar([]()->void{ std::cout << "Hello World!\n"; }); // DOESN'T WORK
}
This is the error I get for trying to construct the bar in main:
Code:
Error 1 error C2664: 'Bar<_Fxn>::Bar(_Fxn (__cdecl *))' : cannot convert parameter 1 from '`anonymous-namespace'::<lambda1>' to 'void (__cdecl *)(void)' c:\projects\_cs350\helix\helix.cpp 30 1 Helix