#include <functional>
template <typename M>
M g(std::function<M(int)> f) {
return f(0);
}
int main() {
g([](int x){return x + 1;});
return 0;
}
I want to express something like "the (only) argument passed to function g
should be a callable object that has int
as type of the parameter".
G++ 9.3.0 says
prog.cc: In function 'int main()':
prog.cc:8:31: error: no matching function for call to 'g(main()::<lambda(int)>)'
8 | g([](int x){return x + 1;});
| ^
prog.cc:3:3: note: candidate: 'template<class M> M g(std::function<M(int)>)'
3 | M g(std::function<M(int)> f) {
| ^
prog.cc:3:3: note: template argument deduction/substitution failed:
prog.cc:8:31: note: 'main()::<lambda(int)>' is not derived from 'std::function<M(int)>'
8 | g([](int x){return x + 1;});
| ^
What is wrong with the above attempt? And how should I achieve that intention?
You may want to see the code snippet on Wandbox here.