blob: 87e7e8462ba59cb8b3fc115b63f7aa1af1a0ff3e [file] [log] [blame]
Sebastian Redl2b916b82012-01-17 22:49:42 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
3namespace std {
4 typedef decltype(sizeof(int)) size_t;
5
6 // libc++'s implementation
7 template <class _E>
8 class initializer_list
9 {
10 const _E* __begin_;
11 size_t __size_;
12
13 initializer_list(const _E* __b, size_t __s)
14 : __begin_(__b),
15 __size_(__s)
16 {}
17
18 public:
19 typedef _E value_type;
20 typedef const _E& reference;
21 typedef const _E& const_reference;
22 typedef size_t size_type;
23
24 typedef const _E* iterator;
25 typedef const _E* const_iterator;
26
27 initializer_list() : __begin_(nullptr), __size_(0) {}
28
29 size_t size() const {return __size_;}
30 const _E* begin() const {return __begin_;}
31 const _E* end() const {return __begin_ + __size_;}
32 };
33}
34
35struct one { char c[1]; };
36struct two { char c[2]; };
37
38struct A {
39 int a, b;
40};
41
42struct B {
43 B();
44 B(int, int);
45};
46
47void simple_list() {
48 std::initializer_list<int> il = { 1, 2, 3 };
49 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
50 std::initializer_list<A> al = { {1, 2}, {2, 3}, {3, 4} };
51 std::initializer_list<B> bl = { {1, 2}, {2, 3}, {} };
52}
53
54void function_call() {
55 void f(std::initializer_list<int>);
56 f({1, 2, 3});
57
58 void g(std::initializer_list<B>);
59 g({ {1, 2}, {2, 3}, {} });
60}
Sebastian Redlfe592282012-01-17 22:49:48 +000061
62struct C {
63 C(int);
64};
65
66struct D {
67 D();
68 operator int();
69 operator C();
70};
71
72void overloaded_call() {
73 one overloaded(std::initializer_list<int>);
74 two overloaded(std::initializer_list<B>);
75
76 static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one), "bad overload");
77 static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two), "bad overload");
78
79 void ambiguous(std::initializer_list<A>); // expected-note {{candidate}}
80 void ambiguous(std::initializer_list<B>); // expected-note {{candidate}}
81 ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}}
82
83 one ov2(std::initializer_list<int>); // expected-note {{candidate}}
84 two ov2(std::initializer_list<C>); // expected-note {{candidate}}
85 // Worst sequence to int is identity, whereas to C it's user-defined.
86 static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one), "bad overload");
87 // But here, user-defined is worst in both cases.
88 ov2({1, 2, D()}); // expected-error {{ambiguous}}
89}