I am trying to make a simple implementation of the std::function
The following code works with function pointers and lambdas which are explicitly converted.
template<typename funct>
class functor {
private:
funct *function;
public:
functor() = default;
functor(funct *func) : function(func) {};
template<typename T>
T operator()(T a, T b){
return function(a, b);
}
};
int add(int a, int b) {
return a + b;
}
int main(int argc, char **argv) {
std::map<std::string, functor<int(int,int)>> maps = { {"+", add} };
maps.insert({ "%", {[](int i, int j)->int { return i * j; } } } );
auto temp = maps["%"](5,6);
std::cout << temp << std::endl;
system("PAUSE");
return 0;
}
I want to know why lambdas don't work with implicit conversion.
maps.insert({ "%", [](int i, int j)->int { return i * j; } } );
The above code doesn't work but the following does:
maps.insert({ "%", {[](int i, int j)->int { return i * j; } } } );
but the std::function
works with {}
and without.