下标迭代器中的lambda [英] lambda inside subscript iterator
问题描述
在下标运算符中包含lambda似乎不适用于g ++和clang.
Having a lambda in a subscript operator seems to be not working for g++ and clang.
这是实现错误还是c ++标准中的不满意"规则?
Is this an implementation error or a "unhappy" rule in c++ standard?
示例:
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[ [](){} ];
我知道属性以"[["开头,但是我想知道"[["(带有一个或多个空格)也可以像这样工作:
I know that attributes starts with "[[" but I am wondering that "[ [" ( with one or more white spaces ) also works like:
void func( int x [ [gnu::unused] ] ) {} // compiles fine! :-(
推荐答案
[dcl.attr.grammar]中对此进行了介绍.具有两个连续的[
是属性,因此您必须将其包装在括号中或执行其他操作以使您的意图清楚:
This is covered in [dcl.attr.grammar]. Having two consecutive [
is an attribute, so you'll have to wrap in parantheses or do something else to make your intent clear:
在引入属性说明符时或在其内部时,两个连续的左方括号标记应仅出现 属性自变量子句的 balanced-token-seq . [注意:如果出现两个连续的左方括号 如果不允许使用属性说明符,则该程序格式不正确,即使括号与替代语法产生符匹配也是如此. -尾注] [示例:
Two consecutive left square bracket tokens shall appear only when introducing an attribute-specifier or within the balanced-token-seq of an attribute-argument-clause. [ Note: If two consecutive left square brackets appear where an attribute-specifier is not allowed, the program is ill-formed even if the brackets match an alternative grammar production. —end note ] [ Example:
int p[10];
void f() {
int x = 42, y[5];
int(p[[x] { return x; }()]); // error: invalid attribute on a nested
// declarator-id and not a function-style cast of
// an element of p.
y[[] { return 2; }()] = 2; // error even though attributes are not allowed
// in this context.
int i [[vendor::attr([[]])]]; // well-formed implementation-defined attribute.
}
-结束示例]
这篇关于下标迭代器中的lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!