据我了解 - 通用 lambda 被转换为带有模板的局部范围结构的对象operator()
。这使得通用 lambda 工具非常强大且易于使用。另一方面,当结构具有模板化成员时,可以创建嵌套到函数中的结构,例如:
#include <iostream>
int main() {
struct inner {
template <class T>
void operator()(T &&i) { }
};
return 0;
}
或自行模板化:
int main() {
template <class T>
struct inner {
void operator()(T &&i) { }
};
return 0;
}
编译器似乎在编译它时有问题:
error: invalid declaration of member template in local class
和
error: a template declaration cannot appear at block scope
我认为问题更多在于 c++ 标准而不是编译器错误。允许 lambda 具有模板化成员而不是本地结构的原因是什么?
我找到了这个 qustion,但我认为答案有点过时(我认为即使对于 c++11 也是如此)。