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