Reputation: 342
According to the standard a built-in subscript expression, where one operand is an array rvalue, is a xvalue (https://en.cppreference.com/w/cpp/language/value_category). But when I wrote a simple test I found out that MSVC considers such expressions as lvalue!
The following code fragment successfully compiles with MSVC:
using T5ElemArray = int[5];
&T5ElemArray{ 1, 2, 3, 4, 5 } [3] ;
T5ElemArray{ 1, 2, 3, 4, 5 } [3] = 77;
int& i = T5ElemArray{ 1, 2, 3, 4, 5 } [3] ;
Clang and g++ finds all three errors in the above code. Does anybody know anything about this bug?
Upvotes: 2
Views: 71
Reputation: 1
This is CWG1213 which msvc hasn't implemented yet. MSVC compiles the invalid program even with /permissive-
flag.
From expr.sub#2:
With the built-in subscript operator, an expression-list shall be present, consisting of a single assignment-expression. One of the expressions shall be a glvalue of type “array of T” or a prvalue of type “pointer to T” and the other shall be a prvalue of unscoped enumeration or integral type. The result is of type “T”. The type “T” shall be a completely-defined object type.50 The expression E1[E2] is identical (by definition) to *((E1)+(E2)), except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise.
(emphasis mine)
The above means that we're not allowed to assign to the expression T5ElemArray{ 1, 2, 3, 4, 5 } [3]
or take its address using &
.
Here is the newly submitted msvc bug:
MSVC compiles assignment to result of substripting an rvalue array
Upvotes: 1