1 // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6 #if __cplusplus < 201103L
7 // expected-no-diagnostics
10 namespace dr1460 { // dr1460: 3.5
11 #if __cplusplus >= 201103L
23 constexpr B() = default;
32 #if __cplusplus > 201103L
33 constexpr void f() { C c; }
34 static_assert((f(), true), "");
39 union B { int n; }; // expected-note +{{here}}
40 union C { int n = 0; };
41 struct D { union {}; };
42 struct E { union { int n; }; }; // expected-note +{{here}}
43 struct F { union { int n = 0; }; };
46 friend constexpr A::A() noexcept;
47 friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}}
48 friend constexpr C::C() noexcept;
49 friend constexpr D::D() noexcept;
50 friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}}
51 friend constexpr F::F() noexcept;
54 // These are OK, because value-initialization doesn't actually invoke the
64 union A { constexpr A() = default; };
65 union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}}
66 union C { int n = 0; constexpr C() = default; };
67 struct D { union {}; constexpr D() = default; };
68 struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}}
69 struct F { union { int n = 0; }; constexpr F() = default; };
71 struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}}
76 union { // expected-note 2{{member not initialized}}
79 constexpr H() {} // expected-error {{must initialize all members}}
80 constexpr H(bool) : m(1) {}
81 constexpr H(char) : n(1) {} // expected-error {{must initialize all members}}
82 constexpr H(double) : m(1), n(1) {}
86 #if __cplusplus > 201103L
87 template<typename T> constexpr bool check() {
88 T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}}
91 static_assert(check<A>(), "");
92 static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}}
93 static_assert(check<C>(), "");
94 static_assert(check<D>(), "");
95 static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}}
96 static_assert(check<F>(), "");
100 int a = 0; // expected-note {{previous initialization is here}}
101 int b = 0; // expected-error {{initializing multiple members of union}}
105 int a = 0; // expected-note {{previous initialization is here}}
108 int b = 0; // expected-error {{initializing multiple members of union}}
113 int a = 0; // expected-note {{previous initialization is here}}
114 int b = 0; // expected-error {{initializing multiple members of union}}
118 union { int a = 0; };
119 union { int b = 0; };
122 namespace Overriding {
125 constexpr A() : b(2) {}
127 static_assert(A().a == 1 && A().b == 2 && A().c == 3, "");
131 constexpr B() : a(1) {}
132 constexpr B(char) : b(4) {}
133 constexpr B(int) : c(3) {}
134 constexpr B(const char*) {}
136 static_assert(B().a == 1, "");
137 static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
138 static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}}
139 static_assert(B('x').b == 4, "");
140 static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
141 static_assert(B(123).c == 3, "");
142 static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
143 static_assert(B("").b == 2, "");
144 static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
147 union { int a, b = 2, c; };
148 union { int d, e = 5, f; };
149 constexpr C() : a(1) {}
150 constexpr C(char) : c(3) {}
151 constexpr C(int) : d(4) {}
152 constexpr C(float) : f(6) {}
153 constexpr C(const char*) {}
156 static_assert(C().a == 1, "");
157 static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
158 static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
159 static_assert(C().e == 5, "");
161 static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
162 static_assert(C('x').c == 3, "");
163 static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
164 static_assert(C('x').e == 5, "");
166 static_assert(C(1).b == 2, "");
167 static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
168 static_assert(C(1).d == 4, "");
169 static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
171 static_assert(C(1.f).b == 2, "");
172 static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
173 static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
174 static_assert(C(1.f).f == 6, "");
176 static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
177 static_assert(C("").b == 2, "");
178 static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
179 static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
180 static_assert(C("").e == 5, "");
181 static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}}
188 int b = const_cast<D&>(d).a = 1; // not evaluated
191 constexpr D() : a(0), c(0) {}
194 static_assert(d.a == 0, "");
199 #if __cplusplus >= 201103L
201 typedef decltype(sizeof(int)) size_t;
203 // libc++'s implementation
205 class initializer_list
210 initializer_list(const _E* __b, size_t __s)
211 : __begin_(__b), __size_(__s) {}
214 typedef _E value_type;
215 typedef const _E& reference;
216 typedef const _E& const_reference;
217 typedef size_t size_type;
219 typedef const _E* iterator;
220 typedef const _E* const_iterator;
222 initializer_list() : __begin_(nullptr), __size_(0) {}
224 size_t size() const {return __size_;}
225 const _E* begin() const {return __begin_;}
226 const _E* end() const {return __begin_ + __size_;}
230 namespace dr1467 { // dr1467: 3.7 c++11
231 // List-initialization of aggregate from same-type object
242 struct SS : public S { } x;
256 struct SS : public S { } x;
270 struct SS : public S { } x;
275 namespace dr_example {
278 OK(const OK&) = default;
287 X(const X&) = default;
294 namespace nonaggregate {
295 struct NonAggregate {
300 WantsIt(NonAggregate);
303 void f(NonAggregate);
318 namespace SelfInitIsNotListInit {
325 S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor
328 struct NestedInit { int a, b, c; };
329 NestedInit ni[1] = {{NestedInit{1, 2, 3}}};
331 namespace NestedInit2 {
332 struct Pair { int a, b; };
333 struct TwoPairs { TwoPairs(Pair, Pair); };
334 struct Value { Value(Pair); Value(TwoPairs); };
335 void f() { Value{{{1,2},{3,4}}}; }
339 namespace dr1490 { // dr1490: 3.7 c++11
340 // List-initialization from a string literal
342 char s[4]{"abc"}; // Ok
343 std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}}
346 namespace dr1495 { // dr1495: 4.0
347 // Deduction succeeds in both directions.
348 template<typename T, typename U> struct A {}; // expected-note {{template is declared here}}
349 template<typename T, typename U> struct A<U, T> {}; // expected-error {{class template partial specialization is not more specialized}}
351 // Primary template is more specialized.
352 template<typename, typename...> struct B {}; // expected-note {{template is declared here}}
353 template<typename ...Ts> struct B<Ts...> {}; // expected-error {{not more specialized}}
355 // Deduction fails in both directions.
356 template<int, typename, typename ...> struct C {}; // expected-note {{template is declared here}}
357 template<typename ...Ts> struct C<0, Ts...> {}; // expected-error {{not more specialized}}
359 #if __cplusplus >= 201402L
360 // Deduction succeeds in both directions.
361 template<typename T, typename U> int a; // expected-note {{template is declared here}}
362 template<typename T, typename U> int a<U, T>; // expected-error {{variable template partial specialization is not more specialized}}
364 // Primary template is more specialized.
365 template<typename, typename...> int b; // expected-note {{template is declared here}}
366 template<typename ...Ts> int b<Ts...>; // expected-error {{not more specialized}}
368 // Deduction fails in both directions.
369 template<int, typename, typename ...> int c; // expected-note {{template is declared here}}
370 template<typename ...Ts> int c<0, Ts...>; // expected-error {{not more specialized}}