• 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
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 bullet2 {
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 {{override}}
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 {{override}}
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   // FIXME: This is an ill-formed narrowing initialization.
66   S s2 { 1.0, 2, 3 };
67   S s3 {};
68 }
69 
70 namespace bullet5 {
71   struct S {
Sbullet5::S72     S(std::initializer_list<double>) {}
Sbullet5::S73     S(const std::string &) {}
74   };
75 
76   const S& r1 = { 1, 2, 3.0 };
77   const S& r2 = { "Spinach" };
78   S& r3 = { 1, 2, 3 };  // expected-error {{non-const lvalue reference to type 'bullet5::S' cannot bind to an initializer list temporary}}
79   const int& i1 = { 1 };
80   const int& i2 = { 1.1 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{override}} expected-warning {{implicit conversion}}
81   const int (&iar)[2] = { 1, 2 };
82 }
83 
84 namespace bullet6 {
85   int x1 {2};
86   int x2 {2.0};  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{override}}
87 }
88 
89 namespace bullet7 {
90   int** pp {};
91 }
92 
93 namespace bullet8 {
94   struct A { int i; int j; };
95   A a1 { 1, 2 };
96   A a2 { 1.2 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{override}} expected-warning {{implicit conversion}}
97 
98   struct B {
Bbullet8::B99     B(std::initializer_list<int> i) {}
100   };
101   B b1 { 1, 2 };
102   B b2 { 1, 2.0 };
103 
104   struct C {
Cbullet8::C105     C(int i, double j) {}
106   };
107   C c1 = { 1, 2.2 };
108   // FIXME: This is an ill-formed narrowing initialization.
109   C c2 = { 1.1, 2 };  // expected-warning {{implicit conversion}}
110 
111   int j { 1 };
112   int k { };
113 }
114