I have the following class that accepts lambda in its constructor
template<typename MAPPER>
class MapHead {
protected:
MAPPER mapper_;
public:
MapHead(MAPPER mapf)
: mapper_(mapf) {}
template<typename IN>
auto call(IN input) -> decltype(mapper_(input)) {
return mapper_(input);
}
};
The purpose of this class is to hold the lambda for lazy evaluation. And I test it like this
auto node = MapHead([](int a) {
^-- Error here
return (uint32_t) a * 2;
});
int value = 2;
EXPECT_EQ(4, node.call(value));
I got error at the mark saying "Use of class template 'MapHead' requires template arguments" when compiling the code using gcc with C++ 14.
As lambda does not have a named type, I cannot manually provide it with a template argument like MapHead<LambdaType>
.
When switching to C++17, the code compiles, as C++17 has automatic template type deduction from lambda. But I see people using lambda as function arguments in C++0x as mentioned in this answer.
My question is: is switching to C++17 the only way to solve this problem? Or is there anything I am missing?