1
votes

§ 2.14.5/8 of the N3337 draft standard states:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

§ 4.2

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

I expected ++"hello world!" to decay "hello world!" to a char* but this does not happen. It must be forced using an extra plus, +++"hello world!". There are some cases where the array-to-pointer conversion is not applied, such as the sizeof operator.

§ 5.3.2 simply says:

The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The result is the updated operand; it is an lvalue, and it is a bit-field if the operand is a bit-field. If x is not of type bool, the expression ++x is equivalent to x+=1

So why doesn't this conversion happen?

1

1 Answers

1
votes

The array-to-pointer (and other decay) conversions are applied when it's necessary to convert a glvalue to a prvalue, as per 5/8:

Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue.

Notice that by your own quoting of 5.3.2, the operand of ++ must be a modifiable lvalue. The array-to-pointer conversion is used to turn a glvalue into a prvalue, which is NOT what ++ needs. Therefore, nothing calls for the array-to-pointer conversion. It would actually be invalid to apply it, as the result of that conversion is a prvalue, on which ++ can not act.