【发布时间】:2017-01-17 15:04:20
【问题描述】:
在下标运算符中包含 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! :-(
【问题讨论】:
标签: c++ c++11 lambda attributes language-lawyer