| 1 |
// Basic uses of initializer lists |
| 2 |
// { dg-do run { target c++11 } } |
| 3 |
|
| 4 |
#include <initializer_list> |
| 5 |
|
| 6 |
extern "C" void abort(); |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
|
| 10 |
struct A { int i,j; A(int _i,int _j): i(_i), j(_j) {} }; |
| 11 |
struct B { A a; B(A _a): a(_a) {} }; |
| 12 |
struct C { B b; C(B _b): b(_b) {} }; |
| 13 |
|
| 14 |
struct D |
| 15 |
{ |
| 16 |
int ia[3]; |
| 17 |
D (initializer_list<int> l) |
| 18 |
{ |
| 19 |
const int *p = l.begin(); |
| 20 |
for (int i = 0; i < 3; ++i) |
| 21 |
ia[i] = *p++; |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
void f(C c) |
| 26 |
{ |
| 27 |
if (c.b.a.i != 1) abort(); |
| 28 |
if (c.b.a.j != 2) abort(); |
| 29 |
} |
| 30 |
void f(int); |
| 31 |
|
| 32 |
void g(D d) |
| 33 |
{ |
| 34 |
if (d.ia[0] != 1 || d.ia[1] != 2 || d.ia[2] != 3) |
| 35 |
abort(); |
| 36 |
} |
| 37 |
|
| 38 |
struct E |
| 39 |
{ |
| 40 |
int i, j, k; |
| 41 |
}; |
| 42 |
|
| 43 |
void h(E e) |
| 44 |
{ |
| 45 |
if (e.i != 1 || e.j != 2 || e.k != 3) |
| 46 |
abort(); |
| 47 |
} |
| 48 |
|
| 49 |
void i(initializer_list<int> l) |
| 50 |
{ |
| 51 |
const int *p = l.begin(); |
| 52 |
if (*p++ != 1) abort(); |
| 53 |
if (*p++ != 2) abort(); |
| 54 |
if (*p++ != 3) abort(); |
| 55 |
if (p != l.end()) abort(); |
| 56 |
} |
| 57 |
|
| 58 |
struct U { U(int, int) {} }; |
| 59 |
U ua[] = { { 3, 2 } }; |
| 60 |
|
| 61 |
int main() |
| 62 |
{ |
| 63 |
g({1,2,3}); |
| 64 |
|
| 65 |
h({1,2,3}); |
| 66 |
|
| 67 |
f({{{1,2}}}); |
| 68 |
f({{A{1,2}}}); |
| 69 |
|
| 70 |
i({1,2,3}); |
| 71 |
} |