|
5991 | 5991 | an initializer list as if
|
5992 | 5992 | the implementation generated and materialized\iref{conv.rval}
|
5993 | 5993 | a prvalue of type ``array of $N$ \tcode{const E}'',
|
5994 |
| -where $N$ is the number of elements in the |
5995 |
| -initializer list. Each element of that array is copy-initialized with the |
| 5994 | +where $N$ is the number of elements in the initializer list; |
| 5995 | +this is called the initializer list's \defnadj{backing}{array}. |
| 5996 | +Each element of the backing array is copy-initialized with the |
5996 | 5997 | corresponding element of the initializer list, and the
|
5997 | 5998 | \tcode{std::initializer_list<E>} object is constructed to refer to that array.
|
5998 | 5999 | \begin{note}
|
5999 | 6000 | A constructor or conversion function selected for the copy is required to be
|
6000 | 6001 | accessible\iref{class.access} in the context of the initializer list.
|
6001 | 6002 | \end{note}
|
6002 |
| -If a narrowing conversion is required to initialize any of the elements, the program is ill-formed. |
| 6003 | +If a narrowing conversion is required to initialize any of the elements, |
| 6004 | +the program is ill-formed. |
| 6005 | +Whether all backing arrays are distinct |
| 6006 | +(that is, are stored in non-overlapping objects) is unspecified. |
| 6007 | + |
| 6008 | +\pnum |
| 6009 | +The backing array has the same lifetime as any other temporary |
| 6010 | +object\iref{class.temporary}, except that initializing an |
| 6011 | +\tcode{initializer_list} object from the array extends the lifetime of |
| 6012 | +the array exactly like binding a reference to a temporary. |
6003 | 6013 | \begin{example}
|
6004 | 6014 | \begin{codeblock}
|
6005 |
| -struct X { |
6006 |
| - X(std::initializer_list<double> v); |
| 6015 | +void f(std::initializer_list<double> il); |
| 6016 | +void g(float x) { |
| 6017 | + f({1, x, 3}); |
| 6018 | +} |
| 6019 | +void h() { |
| 6020 | + f({1, 2, 3}); |
| 6021 | +} |
| 6022 | + |
| 6023 | +struct A { |
| 6024 | + mutable int i; |
6007 | 6025 | };
|
6008 |
| -X x{ 1,2,3 }; |
| 6026 | +void q(std::initializer_list<A>); |
| 6027 | +void r() { |
| 6028 | + q({A{1}, A{2}, A{3}}); |
| 6029 | +} |
6009 | 6030 | \end{codeblock}
|
6010 | 6031 |
|
6011 | 6032 | The initialization will be implemented in a way roughly equivalent to this:
|
6012 | 6033 | \begin{codeblock}
|
6013 |
| -const double __a[3] = {double{1}, double{2}, double{3}}; |
6014 |
| -X x(std::initializer_list<double>(__a, __a+3)); |
| 6034 | +void g(float x) { |
| 6035 | + const double __a[3] = {double{1}, double{x}, double{3}}; // backing array |
| 6036 | + f(std::initializer_list<double>(__a, __a+3)); |
| 6037 | +} |
| 6038 | +void h() { |
| 6039 | + static constexpr double __b[3] = {double{1}, double{2}, double{3}}; // backing array |
| 6040 | + f(std::initializer_list<double>(__b, __b+3)); |
| 6041 | +} |
| 6042 | +void r() { |
| 6043 | + const A __c[3] = {A{1}, A{2}, A{3}}; // backing array |
| 6044 | + q(std::initializer_list<A>(__c, __c+3)); |
| 6045 | +} |
6015 | 6046 | \end{codeblock}
|
6016 |
| -assuming that the implementation can construct an \tcode{initializer_list} object with a pair of pointers. |
| 6047 | +assuming that the implementation |
| 6048 | +can construct an \tcode{initializer_list} object with a pair of pointers, and |
| 6049 | +with the understanding that \tcode{__b} does not outlive the call to \tcode{f}. |
6017 | 6050 | \end{example}
|
6018 | 6051 |
|
6019 |
| -\pnum |
6020 |
| -The array has the same lifetime as any other temporary |
6021 |
| -object\iref{class.temporary}, except that initializing an |
6022 |
| -\tcode{initiali\-zer_list} object from the array extends the lifetime of |
6023 |
| -the array exactly like binding a reference to a temporary. |
6024 | 6052 | \begin{example}
|
6025 | 6053 | \begin{codeblock}
|
6026 | 6054 | typedef std::complex<double> cmplx;
|
|
6047 | 6075 | a temporary array to a reference member, so the program is
|
6048 | 6076 | ill-formed\iref{class.base.init}.
|
6049 | 6077 | \end{example}
|
6050 |
| -\begin{note} |
6051 |
| -The implementation is free to allocate the array in read-only memory if an explicit array with the same initializer can be so allocated. |
6052 |
| -\end{note} |
6053 | 6078 |
|
6054 | 6079 | \pnum
|
6055 | 6080 | A \defnadj{narrowing}{conversion} is an implicit conversion
|
|
0 commit comments