使用lambda表达式作为下标运算符似乎在g++和clang中无法工作。
这是实现错误还是C++标准中的“不规范”规则?
示例:
class A
{
public:
template<typename T> void operator[](T) {}
template<typename T> void operator()(T) {}
};
int main()
{
A a;
a[ [](){} ]; // did not compiler: see error message
a( [](){} ); // works as expected
}
错误:
main.cpp:13:6: error: two consecutive '[' shall only introduce an attribute before '[' token
a[ [](){} ];
^
main.cpp:13:15: error: expected primary-expression before ']' token
a[ [](){} ];
我知道属性以 "[[" 开头,但我想知道是否 "[ ["(带一个或多个空格)也可以像这样使用:
void func( int x [ [gnu::unused] ] ) {} // compiles fine! :-(