cxx0x-initializer-aggregates.cpp revision 20ff0e2d74c188cb3fd4ec3dead41a80a37c0202
1// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
2
3struct one { char c[1]; };
4struct two { char c[2]; };
5
6namespace aggregate {
7  // Direct list initialization does NOT allow braces to be elided!
8  struct S {
9    int ar[2];
10    struct T {
11      int i1;
12      int i2;
13    } t;
14    struct U {
15      int i1;
16    } u[2];
17    struct V {
18      int var[2];
19    } v;
20  };
21
22  void bracing() {
23    S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; // no-error
24    S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
25    S s3{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
26    S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
27    S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
28  }
29
30  void bracing_new() {
31    new S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
32    new S{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
33    new S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
34    new S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
35  }
36
37  void bracing_construct() {
38    (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
39    (void) S{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
40    (void) S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
41    (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
42  }
43
44  struct String {
45    String(const char*);
46  };
47
48  struct A {
49    int m1;
50    int m2;
51  };
52
53  void function_call() {
54    void takes_A(A);
55    takes_A({1, 2});
56  }
57
58  struct B {
59    int m1;
60    String m2;
61  };
62
63  void overloaded_call() {
64    one overloaded(A);
65    two overloaded(B);
66
67    static_assert(sizeof(overloaded({1, 2})) == sizeof(one), "bad overload");
68    static_assert(sizeof(overloaded({1, "two"})) == sizeof(two),
69      "bad overload");
70    // String is not default-constructible
71    static_assert(sizeof(overloaded({1})) == sizeof(one), "bad overload");
72  }
73
74  struct C { int a[2]; C():a({1, 2}) { } }; // expected-error {{array initializer must be an initializer list}}
75}
76