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