I can compile the following code without any problem (using gcc 11.1.0):
#include <iostream> template <typename Func> class K { Func f; public: K(Func _f): f{_f} {}; void do_thing(int x) {f(x);}; }; int main() { auto f = [](int x) {std::cout << x << std::endl;}; K kl{f}; kl.do_thing(5); return 0; } however I would like to perform some check in the constructor of the class K (for instance some std::is_convertible_v inside some bool function), so I tried to modify the code to
#include <iostream> template <typename Func> class K { Func f; public: K(Func _f) { ... f = _f;}; void do_thing(int x) {f(x);}; }; int main() { auto f = [](int x) {std::cout << x << std::endl;}; K kl{f}; kl.do_thing(5); return 0; } which however gives me some error message
error: use of deleted function ‘main()::<lambda(int)>::<lambda>()’ and then
note: a lambda closure type has a deleted default constructor This confuses me a lot since I cannot understand how it is possible that the former piece of code could compile since the lambda function has not default constructor.
Question
How can I set my f inside the body of the constructor? (This is just a MWE and in my case the class is a bit more complex and the checks I mentioned before make sense.)