p3-0x.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
3namespace std {
4  typedef decltype(sizeof(int)) size_t;
5
6  template <typename E>
7  struct initializer_list // expected-note 2{{candidate}}
8  {
9    const E *p;
10    size_t n;
11    initializer_list(const E *p, size_t n) : p(p), n(n) {}
12  };
13
14  struct string {
15    string(const char *);
16  };
17
18  template<typename A, typename B>
19  struct pair {
20    pair(const A&, const B&);
21  };
22}
23
24namespace bullet1 {
25  double ad[] = { 1, 2.0 };
26  int ai[] = { 1, 2.0 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
27
28  struct S2 {
29    int m1;
30    double m2, m3;
31  };
32
33  S2 s21 = { 1, 2, 3.0 };
34  S2 s22 { 1.0, 2, 3 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
35  S2 s23 { };
36}
37
38namespace bullet4_example1 {
39  struct S {
40    S(std::initializer_list<double> d) {}
41    S(std::initializer_list<int> i) {}
42    S() {}
43  };
44
45  S s1 = { 1.0, 2.0, 3.0 };
46  S s2 = { 1, 2, 3 };
47  S s3 = { };
48}
49
50namespace bullet4_example2 {
51  struct Map {
52    Map(std::initializer_list<std::pair<std::string,int>>) {}
53  };
54
55  Map ship = {{"Sophie",14}, {"Surprise",28}};
56}
57
58namespace bullet4_example3 {
59  struct S {
60    S(int, double, double) {}
61    S() {}
62  };
63
64  S s1 = { 1, 2, 3.0 };
65  S s2 { 1.0, 2, 3 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
66  S s3 {};
67}
68
69namespace bullet5 {
70  int x1 {2};
71  int x2 {2.0};  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
72}
73
74namespace bullet6 {
75  struct S {
76    S(std::initializer_list<double>) {}
77    S(const std::string &) {}
78  };
79
80  const S& r1 = { 1, 2, 3.0 };
81  const S& r2 = { "Spinach" };
82  S& r3 = { 1, 2, 3 };  // expected-error {{non-const lvalue reference to type 'bullet6::S' cannot bind to an initializer list temporary}}
83  const int& i1 = { 1 };
84  const int& i2 = { 1.1 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
85  const int (&iar)[2] = { 1, 2 };
86}
87
88namespace bullet7 {
89  int** pp {};
90}
91
92namespace bullet8 {
93  struct A { int i; int j; };
94  A a1 { 1, 2 };
95  A a2 { 1.2 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
96
97  struct B {
98    B(std::initializer_list<int> i) {}
99  };
100  B b1 { 1, 2 };
101  B b2 { 1, 2.0 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
102
103  struct C {
104    C(int i, double j) {}
105  };
106  C c1 = { 1, 2.2 };
107  // FIXME: Suppress the narrowing warning in the cases where we issue a narrowing error.
108  C c2 = { 1.1, 2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
109
110  int j { 1 };
111  int k { };
112}
113
114namespace rdar13395022 {
115  struct MoveOnly {
116    MoveOnly(MoveOnly&&);
117  };
118
119  void test(MoveOnly mo) {
120    // FIXME: These diagnostics are poor.
121    auto &&list1 = {mo}; // expected-error{{no viable conversion}}
122    MoveOnly (&&list2)[1] = {mo}; // expected-error{{no viable conversion}}
123    std::initializer_list<MoveOnly> &&list3 = {};
124    MoveOnly (&&list4)[1] = {}; // expected-error{{uninitialized}}
125  }
126}
127