I'm aware that there's no default constructor for lambda closure type. But does that mean it's impossible to instantiate it after it being passed as a template parameter?
Consider the following minimal example:
#include <iostream>
template <typename FuncType>
std::pair<int,int> DoSomething() {
return FuncType()(std::make_pair(1,1));
}
int main() {
auto myLambda = [](std::pair<int,int> x) {
return std::make_pair(x.first*2,x.second*2);
};
std::pair<int,int> res = DoSomething<decltype(myLambda)>();
return 0;
}
For performance reasons, I can't use std::function
to avoid virtual pointer calls. Is there a way to do this? I need to instantiate that lambda once and use it many times inside that function.
How does the standard library make it work when decltype(myLambda)
is passed to something like std::map
comparators in the template parameter?