|
2416 | 2416 | braced-init-list
|
2417 | 2417 | \end{bnf}
|
2418 | 2418 |
|
| 2419 | +\begin{bnf} |
| 2420 | +\nontermdef{braced-init-list}\br |
| 2421 | + \terminal{\{} initializer-list \terminal{,\opt} \terminal{\}}\br |
| 2422 | + \terminal{\{} designated-initializer-list \terminal{,\opt} \terminal{\}}\br |
| 2423 | + \terminal{\{} \terminal{\}} |
| 2424 | +\end{bnf} |
| 2425 | + |
2419 | 2426 | \begin{bnf}
|
2420 | 2427 | \nontermdef{initializer-list}\br
|
2421 | 2428 | initializer-clause \terminal{...}\opt\br
|
2422 | 2429 | initializer-list \terminal{,} initializer-clause \terminal{...}\opt
|
2423 | 2430 | \end{bnf}
|
2424 | 2431 |
|
2425 | 2432 | \begin{bnf}
|
2426 |
| -\nontermdef{braced-init-list}\br |
2427 |
| - \terminal{\{} initializer-list \terminal{,\opt} \terminal{\}}\br |
2428 |
| - \terminal{\{} \terminal{\}} |
| 2433 | +\nontermdef{designated-initializer-list}\br |
| 2434 | + designated-initializer-clause\br |
| 2435 | + designated-initializer-list \terminal{,} designated-initializer-clause |
| 2436 | +\end{bnf} |
| 2437 | + |
| 2438 | +\begin{bnf} |
| 2439 | +\nontermdef{designated-initializer-clause}\br |
| 2440 | + designator brace-or-equal-initializer |
| 2441 | +\end{bnf} |
| 2442 | + |
| 2443 | +\begin{bnf} |
| 2444 | +\nontermdef{designator}\br |
| 2445 | + \tcode{.} identifier |
2429 | 2446 | \end{bnf}
|
2430 | 2447 |
|
2431 | 2448 | \begin{bnf}
|
|
2894 | 2911 | the expressions are evaluated in the order
|
2895 | 2912 | specified for function calls~(\ref{expr.call}).
|
2896 | 2913 |
|
| 2914 | +\pnum |
| 2915 | +The same \grammarterm{identifier} |
| 2916 | +shall not appear in multiple \grammarterm{designator}{s} of a |
| 2917 | +\grammarterm{designated-initializer-list}. |
| 2918 | + |
2897 | 2919 | \pnum
|
2898 | 2920 | An object whose initialization has completed
|
2899 | 2921 | is deemed to be constructed,
|
|
2960 | 2982 | When an aggregate is initialized by an initializer list
|
2961 | 2983 | as specified in~\ref{dcl.init.list},
|
2962 | 2984 | the elements of the initializer list are taken as initializers
|
2963 |
| -for the elements of the aggregate, in order. |
2964 |
| -Each element is copy-initialized |
2965 |
| -from the corresponding \grammarterm{initializer-clause}. |
2966 |
| -If the \grammarterm{initializer-clause} is an expression and |
| 2985 | +for the elements of the aggregate. |
| 2986 | +The \defnx{explicitly initialized elements}{explicitly initialized elements!aggregate} |
| 2987 | +of the aggregate are determined as follows: |
| 2988 | +\begin{itemize} |
| 2989 | +\item |
| 2990 | +If the initializer list is a \grammarterm{designated-initializer-list}, |
| 2991 | +the aggregate shall be of class type, |
| 2992 | +the \grammarterm{identifier} in each \grammarterm{designator} |
| 2993 | +shall name a direct non-static data member of the class, and |
| 2994 | +the explicitly initialized elements of the aggregate |
| 2995 | +are the elements that are, or contain, those members. |
| 2996 | +\item |
| 2997 | +If the initializer list is an \grammarterm{initializer-list}, |
| 2998 | +the explicitly initialized elements of the aggregate |
| 2999 | +are the first $n$ elements of the aggregate, |
| 3000 | +where $n$ is the number of elements in the initializer list. |
| 3001 | +\item |
| 3002 | +Otherwise, the initializer list must be \tcode{\{\}}, |
| 3003 | +and there are no explicitly initialized elements. |
| 3004 | +\end{itemize} |
| 3005 | + |
| 3006 | +\pnum |
| 3007 | +For each explicitly initialized element: |
| 3008 | +\begin{itemize} |
| 3009 | +\item |
| 3010 | +If the element is an anonymous union object and |
| 3011 | +the initializer list is a \grammarterm{designated-initializer-list}, |
| 3012 | +the anonymous union object is initialized by the |
| 3013 | +\grammarterm{designated-initializer-list} \tcode{\{ }\placeholder{D}\tcode{ \}}, |
| 3014 | +where \placeholder{D} is the \grammarterm{designated-initializer-clause} |
| 3015 | +naming a member of the anonymous union object. |
| 3016 | +There shall be only one such \grammarterm{designated-initializer-clause}. |
| 3017 | +\item |
| 3018 | +Otherwise, the element is copy-initialized |
| 3019 | +from the corresponding \grammarterm{initializer-clause} |
| 3020 | +or the \grammarterm{brace-or-equal-initializer} |
| 3021 | +of the corresponding \grammarterm{designated-initializer-clause}. |
| 3022 | +If that initializer is of the form |
| 3023 | +\grammarterm{assignment-expression} or |
| 3024 | +\tcode{= }\grammarterm{assignment-expression} |
| 3025 | +and |
2967 | 3026 | a narrowing conversion~(\ref{dcl.init.list}) is required
|
2968 | 3027 | to convert the expression, the program is ill-formed.
|
2969 |
| -\begin{note} If an \grammarterm{initializer-clause} is itself an initializer list, |
| 3028 | +\begin{note} If an initializer is itself an initializer list, |
2970 | 3029 | the element is list-initialized, which will result in a recursive application
|
2971 | 3030 | of the rules in this section if the element is an aggregate. \end{note}
|
| 3031 | +\end{itemize} |
2972 | 3032 | \begin{example}
|
2973 | 3033 | \begin{codeblock}
|
2974 | 3034 | struct A {
|
|
3013 | 3073 | \tcode{d2.d} with 4.
|
3014 | 3074 | \end{example}
|
3015 | 3075 |
|
| 3076 | +\pnum |
| 3077 | +For a non-union aggregate, |
| 3078 | +each element that is not an explicitly initialized element |
| 3079 | +is initialized as follows: |
| 3080 | +\begin{itemize} |
| 3081 | +\item |
| 3082 | +If the element has a default member initializer~(\ref{class.mem}), |
| 3083 | +the element is initialized from that initializer. |
| 3084 | +\item |
| 3085 | +Otherwise, if the element is not a reference, the element |
| 3086 | +is copy-initialized from an empty initializer list~(\ref{dcl.init.list}). |
| 3087 | +\item |
| 3088 | +Otherwise, the program is ill-formed. |
| 3089 | +\end{itemize} |
| 3090 | +If the aggregate is a union and the initializer list is empty, then |
| 3091 | +\begin{itemize} |
| 3092 | +\item |
| 3093 | +if any variant member has a default member initializer, |
| 3094 | +that member is initialized from its default member initializer; |
| 3095 | +\item |
| 3096 | +otherwise, the first member of the union (if any) |
| 3097 | +is copy-initialized from an empty initializer list. |
| 3098 | +\end{itemize} |
| 3099 | +\begin{example} |
| 3100 | + |
| 3101 | +\begin{codeblock} |
| 3102 | +struct S { int a; const char* b; int c; int d = b[a]; }; |
| 3103 | +S ss = { 1, "asdf" }; |
| 3104 | +\end{codeblock} |
| 3105 | + |
| 3106 | +initializes |
| 3107 | +\tcode{ss.a} |
| 3108 | +with 1, |
| 3109 | +\tcode{ss.b} |
| 3110 | +with \tcode{"asdf"}, |
| 3111 | +\tcode{ss.c} |
| 3112 | +with the value of an expression of the form |
| 3113 | +\tcode{int\{\}} |
| 3114 | +(that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]} |
| 3115 | +(that is, \tcode{'s'}), and in |
| 3116 | + |
| 3117 | +\begin{codeblock} |
| 3118 | +struct X { int i, j, k = 42; }; |
| 3119 | +X a[] = { 1, 2, 3, 4, 5, 6 }; |
| 3120 | +X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 3121 | +\end{codeblock} |
| 3122 | + |
| 3123 | +\tcode{a} and \tcode{b} have the same value |
| 3124 | + |
| 3125 | +\begin{codeblock} |
| 3126 | +struct A { |
| 3127 | + string a; |
| 3128 | + int b = 42; |
| 3129 | + int c = -1; |
| 3130 | +}; |
| 3131 | +\end{codeblock} |
| 3132 | + |
| 3133 | +\tcode{A\{.c=21\}} has the following steps: |
| 3134 | +\begin{itemize} |
| 3135 | +\item Initialize \tcode{a} with \tcode{\{\}} |
| 3136 | +\item Initialize \tcode{b} with \tcode{= 42} |
| 3137 | +\item Initialize \tcode{c} with \tcode{= 21} |
| 3138 | +\end{itemize} |
| 3139 | +\end{example} |
| 3140 | + |
| 3141 | +\pnum |
| 3142 | +The initializations of the elements of the aggregate |
| 3143 | +are evaluated in the element order. |
| 3144 | +That is, |
| 3145 | +all value computations and side effects associated with a given element |
| 3146 | +are sequenced before |
| 3147 | +those of any element that follows it in order. |
| 3148 | + |
3016 | 3149 | \pnum
|
3017 | 3150 | An aggregate that is a class can also be initialized with a single
|
3018 | 3151 | expression not enclosed in braces, as described in~\ref{dcl.init}.
|
|
3063 | 3196 |
|
3064 | 3197 | \pnum
|
3065 | 3198 | \begin{note}
|
3066 |
| -Static data members and unnamed bit-fields are not considered |
3067 |
| -elements of the aggregate. |
| 3199 | +Static data members, |
| 3200 | +non-static data members of anonymous union members, |
| 3201 | +and |
| 3202 | +unnamed bit-fields |
| 3203 | +are not considered elements of the aggregate. |
3068 | 3204 | \begin{example}
|
3069 | 3205 |
|
3070 | 3206 | \begin{codeblock}
|
|
3090 | 3226 | \grammarterm{initializer-list}
|
3091 | 3227 | is ill-formed if the number of
|
3092 | 3228 | \grammarterm{initializer-clause}{s}
|
3093 |
| -exceeds the number of elements to initialize. |
| 3229 | +exceeds the number of elements of the aggregate. |
3094 | 3230 | \begin{example}
|
3095 | 3231 |
|
3096 | 3232 | \begin{codeblock}
|
|
3100 | 3236 | is ill-formed.
|
3101 | 3237 | \end{example}
|
3102 | 3238 |
|
3103 |
| -\pnum |
3104 |
| -If there are fewer \grammarterm{initializer-clause}{s} in the list than there |
3105 |
| -are elements in a non-union aggregate, then each element not explicitly initialized |
3106 |
| -is initialized as follows: |
3107 |
| -\begin{itemize} |
3108 |
| -\item |
3109 |
| -If the element has a default member initializer~(\ref{class.mem}), |
3110 |
| -the element is initialized from that initializer. |
3111 |
| -\item |
3112 |
| -Otherwise, if the element is not a reference, the element |
3113 |
| -is copy-initialized from an empty initializer list~(\ref{dcl.init.list}). |
3114 |
| -\item |
3115 |
| -Otherwise, the program is ill-formed. |
3116 |
| -\end{itemize} |
3117 |
| -If the aggregate is a union and the initializer list is empty, then |
3118 |
| -\begin{itemize} |
3119 |
| -\item |
3120 |
| -if any variant member has a default member initializer, |
3121 |
| -that member is initialized from its default member initializer; |
3122 |
| -\item |
3123 |
| -otherwise, the first member of the union (if any) |
3124 |
| -is copy-initialized from an empty initializer list. |
3125 |
| -\end{itemize} |
3126 |
| -\begin{example} |
3127 |
| - |
3128 |
| -\begin{codeblock} |
3129 |
| -struct S { int a; const char* b; int c; int d = b[a]; }; |
3130 |
| -S ss = { 1, "asdf" }; |
3131 |
| -\end{codeblock} |
3132 |
| - |
3133 |
| -initializes |
3134 |
| -\tcode{ss.a} |
3135 |
| -with 1, |
3136 |
| -\tcode{ss.b} |
3137 |
| -with \tcode{"asdf"}, |
3138 |
| -\tcode{ss.c} |
3139 |
| -with the value of an expression of the form |
3140 |
| -\tcode{int\{\}} |
3141 |
| -(that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]} |
3142 |
| -(that is, \tcode{'s'}), and in |
3143 |
| - |
3144 |
| -\begin{codeblock} |
3145 |
| -struct X { int i, j, k = 42; }; |
3146 |
| -X a[] = { 1, 2, 3, 4, 5, 6 }; |
3147 |
| -X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
3148 |
| -\end{codeblock} |
3149 |
| - |
3150 |
| -\tcode{a} and \tcode{b} have the same value |
3151 |
| -\end{example} |
3152 |
| - |
3153 | 3239 | \pnum
|
3154 | 3240 | If a reference member is initialized from its default member initializer
|
3155 | 3241 | and a potentially-evaluated subexpression thereof is an aggregate
|
|
3374 | 3460 |
|
3375 | 3461 | \pnum
|
3376 | 3462 | \indextext{initialization!\idxcode{union}}%
|
3377 |
| -When a union is initialized with a brace-enclosed initializer, |
3378 |
| -the braces shall only contain an |
3379 |
| -\grammarterm{initializer-clause} |
3380 |
| -for the first non-static data member of the union. |
| 3463 | +When a union is initialized with an initializer list, |
| 3464 | +there shall not be more than one |
| 3465 | +explicitly initialized element. |
3381 | 3466 | \begin{example}
|
3382 |
| - |
3383 | 3467 | \begin{codeblock}
|
3384 | 3468 | union u { int a; const char* b; };
|
3385 | 3469 | u a = { 1 };
|
3386 | 3470 | u b = a;
|
3387 | 3471 | u c = 1; // error
|
3388 | 3472 | u d = { 0, "asdf" }; // error
|
3389 | 3473 | u e = { "asdf" }; // error
|
| 3474 | +u f = { .b = "asdf" }; |
| 3475 | +u g = { .a = 1, .b = "asdf" }; // error |
3390 | 3476 | \end{codeblock}
|
3391 | 3477 | \end{example}
|
3392 | 3478 |
|
|
3730 | 3816 |
|
3731 | 3817 | \pnum
|
3732 | 3818 | \defnx{List-initialization}{list-initialization} is initialization of an object or reference from a
|
3733 |
| -\grammarterm{braced-init-list}. Such an initializer is called an \term{initializer |
3734 |
| -list}, and the comma-separated \grammarterm{initializer-clause}{s} of the list are |
3735 |
| -called the \term{elements} of the initializer list. An initializer list may be empty. |
| 3819 | +\grammarterm{braced-init-list}. |
| 3820 | +Such an initializer is called an \term{initializer list}, and |
| 3821 | +the comma-separated |
| 3822 | +\grammarterm{initializer-clause}{s} |
| 3823 | +of the \grammarterm{initializer-list} |
| 3824 | +or |
| 3825 | +\grammarterm{designated-initializer-clause}{s} |
| 3826 | +of the \grammarterm{designated-initializer-list} |
| 3827 | +are called the \term{elements} of the initializer list. An initializer list may be empty. |
3736 | 3828 | List-initialization can occur in direct-initialization or copy-initialization contexts;
|
3737 | 3829 | list-initialization in a direct-initialization context is called
|
3738 | 3830 | \defn{direct-list-initialization} and list-initialization in a
|
|
3784 | 3876 | \pnum
|
3785 | 3877 | List-initialization of an object or reference of type \tcode{T} is defined as follows:
|
3786 | 3878 | \begin{itemize}
|
| 3879 | +\item |
| 3880 | +If the \grammarterm{braced-init-list} |
| 3881 | +contains a \grammarterm{designated-initializer-list}, |
| 3882 | +\tcode{T} shall be an aggregate class. |
| 3883 | +The ordered \grammarterm{identifier}{s} |
| 3884 | +in the \grammarterm{designator}{s} |
| 3885 | +of the \grammarterm{designated-initializer-list} |
| 3886 | +shall form a subsequence |
| 3887 | +of the ordered \grammarterm{identifier}{s} |
| 3888 | +in the direct non-static data members of \tcode{T}. |
| 3889 | +Aggregate initialization is performed~(\ref{dcl.init.aggr}). |
| 3890 | +\begin{example} |
| 3891 | +\begin{codeblock} |
| 3892 | +struct A { int x; int y; int z; }; |
| 3893 | +A a{.y = 2, .x = 1}; // error: designator order does not match declaration order |
| 3894 | +A b{.x = 1, .z = 2}; // OK, \tcode{b.y} initialized to \tcode{0} |
| 3895 | +\end{codeblock} |
| 3896 | +\end{example} |
3787 | 3897 |
|
3788 | 3898 | \item If \tcode{T} is an aggregate class and the initializer list has a single element
|
3789 | 3899 | of type \cvqual{cv} \tcode{U},
|
|
0 commit comments