1

Trying to pass a lambda to a constructor:

#include <functional> #include <exception> template<typename R> class Nisse { private: Nisse(Nisse const&) = delete; Nisse(Nisse&&) = delete; Nisse& operator=(Nisse const&) = delete; Nisse& operator=(Nisse&&) = delete; public: Nisse(std::function<R> const& func) {} }; int main() { Nisse<int> nisse([](){return 5;}); } 

When I compile I get an error message:

Test.cpp: In function ‘int main()’: Test.cpp:19:39: error: no matching function for call to ‘Nisse<int>::Nisse(main()::<lambda()>)’ Test.cpp:19:39: note: candidate is: Test.cpp:14:9: note: Nisse<R>::Nisse(const std::function<R>&) [with R = int] Test.cpp:14:9: note: no known conversion for argument 1 from ‘main()::<lambda()>’ to ‘const std::function<int>&’ 

1 Answer 1

5

The type of the template arg to std::function is wrong. Try using

Nisse(std::function<R()> const& func) {} 

Specifically, the template argument needs to be a function type, but all you were passing was the desired return type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Still geting me teeth in C++11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.