Search code examples
clanguage-lawyercompound-literals

Postfix expression (type-name){initializer-list}


The section 6.5.2.5/4 provides an explanation about the postfix-expression of the form ( type-name ) { initializer-list }. Here it is:

If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an object type), the type of the compound literal is that specified by the type name. In either case, the result is an lvalue.

I don't understand the wording the type of the compound literal. How is it even possible for a literal to have a type? Does the type of the corresponding unnamed object is meant by the type of the compound literal?

For instance

long long *longs = (long long []) {1333, 3123, 3, 122};

The initializer-list here is used to initialize an unnamed object of type long long [4].

Also it is not clear what is the purpose of In either case, the result is an lvalue. When using in an assignment-expression the lvalue conversion is performed on the right operand so it is no longer an lvalue.


Solution

  • Presumably, a "compound literal" means the object/value that a use of the "compound literal" language construct designates. Values/objects have types in C.

    The purpose of making compound literals lvalues is so that code like int *x = &(int){42}; *x = 43; works. It makes compound literals behave kind of like anonymous variables. (Not completely, though. Unlike regular variables, compound literals can't have storage class specifiers, which I personally consider a language defect.)