• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 
3 namespace 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;
initializer_liststd::initializer_list11     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 
24 namespace 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 
38 namespace bullet4_example1 {
39   struct S {
Sbullet4_example1::S40     S(std::initializer_list<double> d) {}
Sbullet4_example1::S41     S(std::initializer_list<int> i) {}
Sbullet4_example1::S42     S() {}
43   };
44 
45   S s1 = { 1.0, 2.0, 3.0 };
46   S s2 = { 1, 2, 3 };
47   S s3 = { };
48 }
49 
50 namespace bullet4_example2 {
51   struct Map {
Mapbullet4_example2::Map52     Map(std::initializer_list<std::pair<std::string,int>>) {}
53   };
54 
55   Map ship = {{"Sophie",14}, {"Surprise",28}};
56 }
57 
58 namespace bullet4_example3 {
59   struct S {
Sbullet4_example3::S60     S(int, double, double) {}
Sbullet4_example3::S61     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 
69 namespace 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 
74 namespace bullet6 {
75   struct S {
Sbullet6::S76     S(std::initializer_list<double>) {}
Sbullet6::S77     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 
88 namespace bullet7 {
89   int** pp {};
90 }
91 
92 namespace 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 {
Bbullet8::B98     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 {
Cbullet8::C104     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 
114 namespace rdar13395022 {
115   struct MoveOnly {
116     MoveOnly(MoveOnly&&);
117   };
118 
test(MoveOnly mo)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