]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/doc/gcc/extensions-to-the-c-language-family/compound-literals.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c-language-family / compound-literals.rst
CommitLineData
c63539ff
ML
1..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6.. index:: constructor expressions, initializations in expressions, structures, constructor expression, expressions, constructor, compound literals
7
8.. _compound-literals:
9
10Compound Literals
11*****************
12
13.. The GNU C name for what C99 calls compound literals was "constructor expressions".
14
15A compound literal looks like a cast of a brace-enclosed aggregate
16initializer list. Its value is an object of the type specified in
17the cast, containing the elements specified in the initializer.
18Unlike the result of a cast, a compound literal is an lvalue. ISO
19C99 and later support compound literals. As an extension, GCC
20supports compound literals also in C90 mode and in C++, although
21as explained below, the C++ semantics are somewhat different.
22
23Usually, the specified type of a compound literal is a structure. Assume
24that ``struct foo`` and ``structure`` are declared as shown:
25
26.. code-block:: c++
27
28 struct foo {int a; char b[2];} structure;
29
30Here is an example of constructing a ``struct foo`` with a compound literal:
31
32.. code-block:: c++
33
34 structure = ((struct foo) {x + y, 'a', 0});
35
36This is equivalent to writing the following:
37
38.. code-block:: c++
39
40 {
41 struct foo temp = {x + y, 'a', 0};
42 structure = temp;
43 }
44
45You can also construct an array, though this is dangerous in C++, as
46explained below. If all the elements of the compound literal are
47(made up of) simple constant expressions suitable for use in
48initializers of objects of static storage duration, then the compound
49literal can be coerced to a pointer to its first element and used in
50such an initializer, as shown here:
51
52.. code-block:: c++
53
54 char **foo = (char *[]) { "x", "y", "z" };
55
56Compound literals for scalar types and union types are also allowed. In
57the following example the variable ``i`` is initialized to the value
58``2``, the result of incrementing the unnamed object created by
59the compound literal.
60
61.. code-block:: c++
62
63 int i = ++(int) { 1 };
64
65As a GNU extension, GCC allows initialization of objects with static storage
66duration by compound literals (which is not possible in ISO C99 because
67the initializer is not a constant).
68It is handled as if the object were initialized only with the brace-enclosed
69list if the types of the compound literal and the object match.
70The elements of the compound literal must be constant.
71If the object being initialized has array type of unknown size, the size is
72determined by the size of the compound literal.
73
74.. code-block:: c++
75
76 static struct foo x = (struct foo) {1, 'a', 'b'};
77 static int y[] = (int []) {1, 2, 3};
78 static int z[] = (int [3]) {1};
79
80The above lines are equivalent to the following:
81
82.. code-block:: c++
83
84 static struct foo x = {1, 'a', 'b'};
85 static int y[] = {1, 2, 3};
86 static int z[] = {1, 0, 0};
87
88In C, a compound literal designates an unnamed object with static or
89automatic storage duration. In C++, a compound literal designates a
90temporary object that only lives until the end of its full-expression.
91As a result, well-defined C code that takes the address of a subobject
92of a compound literal can be undefined in C++, so G++ rejects
93the conversion of a temporary array to a pointer. For instance, if
94the array compound literal example above appeared inside a function,
95any subsequent use of ``foo`` in C++ would have undefined behavior
96because the lifetime of the array ends after the declaration of ``foo``.
97
98As an optimization, G++ sometimes gives array compound literals longer
99lifetimes: when the array either appears outside a function or has
100a ``const`` -qualified type. If ``foo`` and its initializer had
101elements of type ``char *const`` rather than ``char *``, or if
102``foo`` were a global variable, the array would have static storage
103duration. But it is probably safest just to avoid the use of array
3ed1b4ce 104compound literals in C++ code.