xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx0x-initializer-scalars.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct one { char c[1]; };
4*f4a2713aSLionel Sambuc struct two { char c[2]; };
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc namespace std {
7*f4a2713aSLionel Sambuc   typedef decltype(sizeof(int)) size_t;
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc   // libc++'s implementation
10*f4a2713aSLionel Sambuc   template <class _E>
11*f4a2713aSLionel Sambuc   class initializer_list
12*f4a2713aSLionel Sambuc   {
13*f4a2713aSLionel Sambuc     const _E* __begin_;
14*f4a2713aSLionel Sambuc     size_t    __size_;
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc     initializer_list(const _E* __b, size_t __s)
17*f4a2713aSLionel Sambuc       : __begin_(__b),
18*f4a2713aSLionel Sambuc         __size_(__s)
19*f4a2713aSLionel Sambuc     {}
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc   public:
22*f4a2713aSLionel Sambuc     typedef _E        value_type;
23*f4a2713aSLionel Sambuc     typedef const _E& reference;
24*f4a2713aSLionel Sambuc     typedef const _E& const_reference;
25*f4a2713aSLionel Sambuc     typedef size_t    size_type;
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc     typedef const _E* iterator;
28*f4a2713aSLionel Sambuc     typedef const _E* const_iterator;
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc     initializer_list() : __begin_(nullptr), __size_(0) {}
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc     size_t    size()  const {return __size_;}
33*f4a2713aSLionel Sambuc     const _E* begin() const {return __begin_;}
34*f4a2713aSLionel Sambuc     const _E* end()   const {return __begin_ + __size_;}
35*f4a2713aSLionel Sambuc   };
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc namespace integral {
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc   void initialization() {
41*f4a2713aSLionel Sambuc     { const int a{}; static_assert(a == 0, ""); }
42*f4a2713aSLionel Sambuc     { const int a = {}; static_assert(a == 0, ""); }
43*f4a2713aSLionel Sambuc     { const int a{1}; static_assert(a == 1, ""); }
44*f4a2713aSLionel Sambuc     { const int a = {1}; static_assert(a == 1, ""); }
45*f4a2713aSLionel Sambuc     { const int a{1, 2}; } // expected-error {{excess elements}}
46*f4a2713aSLionel Sambuc     { const int a = {1, 2}; } // expected-error {{excess elements}}
47*f4a2713aSLionel Sambuc     // FIXME: Redundant warnings.
48*f4a2713aSLionel Sambuc     { const short a{100000}; } // expected-error {{cannot be narrowed}} expected-note {{inserting an explicit cast}} expected-warning {{changes value}}
49*f4a2713aSLionel Sambuc     { const short a = {100000}; } // expected-error {{cannot be narrowed}} expected-note {{inserting an explicit cast}} expected-warning {{changes value}}
50*f4a2713aSLionel Sambuc     { if (const int a{1}) static_assert(a == 1, ""); }
51*f4a2713aSLionel Sambuc     { if (const int a = {1}) static_assert(a == 1, ""); }
52*f4a2713aSLionel Sambuc   }
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   int direct_usage() {
55*f4a2713aSLionel Sambuc     int ar[10];
56*f4a2713aSLionel Sambuc     (void) ar[{1}]; // expected-error {{array subscript is not an integer}}
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc     return {1};
59*f4a2713aSLionel Sambuc   }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc   void inline_init() {
62*f4a2713aSLionel Sambuc     auto v = int{1};
63*f4a2713aSLionel Sambuc     (void) new int{1};
64*f4a2713aSLionel Sambuc   }
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc   struct A {
67*f4a2713aSLionel Sambuc     int i;
68*f4a2713aSLionel Sambuc     A() : i{1} {}
69*f4a2713aSLionel Sambuc   };
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc   void function_call() {
72*f4a2713aSLionel Sambuc     void takes_int(int);
73*f4a2713aSLionel Sambuc     takes_int({1});
74*f4a2713aSLionel Sambuc   }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc   void overloaded_call() {
77*f4a2713aSLionel Sambuc     one overloaded(int);
78*f4a2713aSLionel Sambuc     two overloaded(double);
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc     static_assert(sizeof(overloaded({0})) == sizeof(one), "bad overload");
81*f4a2713aSLionel Sambuc     static_assert(sizeof(overloaded({0.0})) == sizeof(two), "bad overload");
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc     void ambiguous(int, double); // expected-note {{candidate}}
84*f4a2713aSLionel Sambuc     void ambiguous(double, int); // expected-note {{candidate}}
85*f4a2713aSLionel Sambuc     ambiguous({0}, {0}); // expected-error {{ambiguous}}
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc     void emptylist(int);
88*f4a2713aSLionel Sambuc     void emptylist(int, int, int);
89*f4a2713aSLionel Sambuc     emptylist({});
90*f4a2713aSLionel Sambuc     emptylist({}, {}, {});
91*f4a2713aSLionel Sambuc   }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc   void edge_cases() {
94*f4a2713aSLionel Sambuc     // FIXME: very poor error message
95*f4a2713aSLionel Sambuc     int a({0}); // expected-error {{cannot initialize}}
96*f4a2713aSLionel Sambuc     (void) int({0}); // expected-error {{functional-style cast}}
97*f4a2713aSLionel Sambuc     new int({0});  // expected-error {{cannot initialize}}
98*f4a2713aSLionel Sambuc   }
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc   void default_argument(int i = {}) {
101*f4a2713aSLionel Sambuc   }
102*f4a2713aSLionel Sambuc   struct DefaultArgument {
103*f4a2713aSLionel Sambuc     void default_argument(int i = {}) {
104*f4a2713aSLionel Sambuc     }
105*f4a2713aSLionel Sambuc   };
106*f4a2713aSLionel Sambuc }
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc namespace PR12118 {
109*f4a2713aSLionel Sambuc   void test() {
110*f4a2713aSLionel Sambuc     one f(std::initializer_list<int>);
111*f4a2713aSLionel Sambuc     two f(int);
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc     // to initializer_list is preferred
114*f4a2713aSLionel Sambuc     static_assert(sizeof(f({0})) == sizeof(one), "bad overload");
115*f4a2713aSLionel Sambuc   }
116*f4a2713aSLionel Sambuc }
117