我目前正在尝试使用类模板编程,并且遇到了这种奇怪的行为,在将命名的lambda作为参数传递时我无法理解。有人可以解释为什么下面的(1)和(2)不起作用吗?
template<typename Predicate>
class Test{
public:
Test(Predicate p) : _pred(p) {}
private:
Predicate _pred;
};
int main(){
auto isEven = [](const auto& x){ return x%2 == 0; };
// Working cases
Test([](const auto& x){ return x%2 == 0; });
Test{isEven};
auto testObject = Test(isEven);
// Compilation Error cases
Test(isEven); // (1) Why??? Most vexing parse? not assigned to a variable? I cant understand why this fails to compile.
Test<decltype(isEven)>(isEven); // (2) Basically same as (1) but with a workaround. I'm …
Run Code Online (Sandbox Code Playgroud)