What "alternative grammar" does [[appear besides attributes?

I do not understand this:

(7.6.1) Two tokens of the left left square bracket should appear only when entering the attribute-specifier. [Note: if two consecutive brackets appear on the left, where the specifier attribute is not allowed, the program is poorly formed , even if the brackets correspond to an alternative to grammatical production. -end note] [Example: (slightly modified from source)

// ... void f() { int x = 42, y[5]; // ... y[[] { return 2; }()] = 2; // error even though attributes are not allowed // in this context. } 

What alternative grammar can I use for [[ ? Will the example be valid if the attributes do not exist (and what does this example do)?

+6
source share
1 answer

In the example, a simple lambda is created, which is directly called and will simply return 2 . This will get the third element from the array and assign it 2 . It can be rewritten as follows:

 int foo(){ return 2; } int y[5]; y[foo()] = 2; 

Or even

 int y[5]; auto foo = []{ return 2; }; // create lambda y[foo()] = 2; // call lambda 

Now, if the attributes do not exist, the example, of course, will be well-formed, because the section that you quoted will not exist.

+2
source

Source: https://habr.com/ru/post/902433/


All Articles