gcc: Compound Literals

1 
1 6.26 Compound Literals
1 ======================
1 
1 A compound literal looks like a cast of a brace-enclosed aggregate
1 initializer list.  Its value is an object of the type specified in the
1 cast, containing the elements specified in the initializer.  Unlike the
1 result of a cast, a compound literal is an lvalue.  ISO C99 and later
1 support compound literals.  As an extension, GCC supports compound
1 literals also in C90 mode and in C++, although as explained below, the
1 C++ semantics are somewhat different.
1 
1  Usually, the specified type of a compound literal is a structure.
1 Assume that 'struct foo' and 'structure' are declared as shown:
1 
1      struct foo {int a; char b[2];} structure;
1 
1 Here is an example of constructing a 'struct foo' with a compound
1 literal:
1 
1      structure = ((struct foo) {x + y, 'a', 0});
1 
1 This is equivalent to writing the following:
1 
1      {
1        struct foo temp = {x + y, 'a', 0};
1        structure = temp;
1      }
1 
1  You can also construct an array, though this is dangerous in C++, as
1 explained below.  If all the elements of the compound literal are (made
1 up of) simple constant expressions suitable for use in initializers of
1 objects of static storage duration, then the compound literal can be
1 coerced to a pointer to its first element and used in such an
1 initializer, as shown here:
1 
1      char **foo = (char *[]) { "x", "y", "z" };
1 
1  Compound literals for scalar types and union types are also allowed.
1 In the following example the variable 'i' is initialized to the value
1 '2', the result of incrementing the unnamed object created by the
1 compound literal.
1 
1      int i = ++(int) { 1 };
1 
1  As a GNU extension, GCC allows initialization of objects with static
1 storage duration by compound literals (which is not possible in ISO C99
1 because the initializer is not a constant).  It is handled as if the
1 object were initialized only with the brace-enclosed list if the types
1 of the compound literal and the object match.  The elements of the
1 compound literal must be constant.  If the object being initialized has
1 array type of unknown size, the size is determined by the size of the
1 compound literal.
1 
1      static struct foo x = (struct foo) {1, 'a', 'b'};
1      static int y[] = (int []) {1, 2, 3};
1      static int z[] = (int [3]) {1};
1 
1 The above lines are equivalent to the following:
1      static struct foo x = {1, 'a', 'b'};
1      static int y[] = {1, 2, 3};
1      static int z[] = {1, 0, 0};
1 
1  In C, a compound literal designates an unnamed object with static or
1 automatic storage duration.  In C++, a compound literal designates a
1 temporary object that only lives until the end of its full-expression.
1 As a result, well-defined C code that takes the address of a subobject
1 of a compound literal can be undefined in C++, so G++ rejects the
1 conversion of a temporary array to a pointer.  For instance, if the
1 array compound literal example above appeared inside a function, any
1 subsequent use of 'foo' in C++ would have undefined behavior because the
1 lifetime of the array ends after the declaration of 'foo'.
1 
1  As an optimization, G++ sometimes gives array compound literals longer
1 lifetimes: when the array either appears outside a function or has a
1 'const'-qualified type.  If 'foo' and its initializer had elements of
1 type 'char *const' rather than 'char *', or if 'foo' were a global
1 variable, the array would have static storage duration.  But it is
1 probably safest just to avoid the use of array compound literals in C++
1 code.
1