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++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 5 // RUN: %clang_cc1 -std=c++2a %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 6 7 #if __cplusplus < 201103L 8 // expected-no-diagnostics 9 #endif 10 11 // dr1425: na abi 12 13 namespace dr1460 { // dr1460: 3.5 14 #if __cplusplus >= 201103L 15 namespace DRExample { 16 union A { 17 union {}; // expected-error {{does not declare anything}} 18 union {}; // expected-error {{does not declare anything}} A()19 constexpr A() {} 20 }; 21 constexpr A a = A(); 22 23 union B { 24 union {}; // expected-error {{does not declare anything}} 25 union {}; // expected-error {{does not declare anything}} 26 constexpr B() = default; 27 }; 28 constexpr B b = B(); 29 30 union C { 31 union {}; // expected-error {{does not declare anything}} 32 union {}; // expected-error {{does not declare anything}} 33 }; 34 constexpr C c = C(); 35 #if __cplusplus > 201103L f()36 constexpr void f() { C c; } 37 static_assert((f(), true), ""); 38 #endif 39 } 40 41 union A {}; 42 union B { int n; }; // expected-note 0+{{here}} 43 union C { int n = 0; }; 44 struct D { union {}; }; // expected-error {{does not declare anything}} 45 struct E { union { int n; }; }; // expected-note 0+{{here}} 46 struct F { union { int n = 0; }; }; 47 48 struct X { 49 friend constexpr A::A() noexcept; 50 friend constexpr B::B() noexcept; 51 #if __cplusplus <= 201703L 52 // expected-error@-2 {{follows non-constexpr declaration}} 53 #endif 54 friend constexpr C::C() noexcept; 55 friend constexpr D::D() noexcept; 56 friend constexpr E::E() noexcept; 57 #if __cplusplus <= 201703L 58 // expected-error@-2 {{follows non-constexpr declaration}} 59 #endif 60 friend constexpr F::F() noexcept; 61 }; 62 63 // These are OK, because value-initialization doesn't actually invoke the 64 // constructor. 65 constexpr A a = A(); 66 constexpr B b = B(); 67 constexpr C c = C(); 68 constexpr D d = D(); 69 constexpr E e = E(); 70 constexpr F f = F(); 71 72 namespace Defaulted { 73 union A { constexpr A() = default; }; 74 union B { int n; constexpr B() = default; }; 75 #if __cplusplus <= 201703L 76 // expected-error@-2 {{not constexpr}} 77 #endif 78 union C { int n = 0; constexpr C() = default; }; 79 struct D { union {}; constexpr D() = default; }; // expected-error {{does not declare anything}} 80 struct E { union { int n; }; constexpr E() = default; }; 81 #if __cplusplus <= 201703L 82 // expected-error@-2 {{not constexpr}} 83 #endif 84 struct F { union { int n = 0; }; constexpr F() = default; }; 85 86 struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; 87 #if __cplusplus <= 201703L 88 // expected-error@-2 {{not constexpr}} 89 #endif 90 struct H { 91 union { 92 int n = 0; 93 }; 94 union { // expected-note 0-2{{member not initialized}} 95 int m; 96 }; Hdr1460::Defaulted::H97 constexpr H() {} 98 #if __cplusplus <= 201703L 99 // expected-error@-2 {{initialize all members}} 100 #endif Hdr1460::Defaulted::H101 constexpr H(bool) : m(1) {} Hdr1460::Defaulted::H102 constexpr H(char) : n(1) {} 103 #if __cplusplus <= 201703L 104 // expected-error@-2 {{initialize all members}} 105 #endif Hdr1460::Defaulted::H106 constexpr H(double) : m(1), n(1) {} 107 }; 108 } 109 110 #if __cplusplus > 201103L check()111 template<typename T> constexpr bool check() { 112 T t; 113 #if __cplusplus <= 201703L 114 // expected-note-re@-2 2{{non-constexpr constructor '{{[BE]}}'}} 115 #endif 116 return true; 117 } 118 static_assert(check<A>(), ""); 119 static_assert(check<B>(), ""); 120 #if __cplusplus <= 201703L 121 // expected-error@-2 {{constant}} expected-note@-2 {{in call}} 122 #endif 123 static_assert(check<C>(), ""); 124 static_assert(check<D>(), ""); 125 static_assert(check<E>(), ""); 126 #if __cplusplus <= 201703L 127 // expected-error@-2 {{constant}} expected-note@-2 {{in call}} 128 #endif 129 static_assert(check<F>(), ""); 130 #endif 131 132 union G { 133 int a = 0; // expected-note {{previous initialization is here}} 134 int b = 0; // expected-error {{initializing multiple members of union}} 135 }; 136 union H { 137 union { 138 int a = 0; // expected-note {{previous initialization is here}} 139 }; 140 union { 141 int b = 0; // expected-error {{initializing multiple members of union}} 142 }; 143 }; 144 struct I { 145 union { 146 int a = 0; // expected-note {{previous initialization is here}} 147 int b = 0; // expected-error {{initializing multiple members of union}} 148 }; 149 }; 150 struct J { 151 union { int a = 0; }; 152 union { int b = 0; }; 153 }; 154 155 namespace Overriding { 156 struct A { 157 int a = 1, b, c = 3; Adr1460::Overriding::A158 constexpr A() : b(2) {} 159 }; 160 static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 161 162 union B { 163 int a, b = 2, c; B()164 constexpr B() : a(1) {} B(char)165 constexpr B(char) : b(4) {} B(int)166 constexpr B(int) : c(3) {} B(const char *)167 constexpr B(const char*) {} 168 }; 169 static_assert(B().a == 1, ""); 170 static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 171 static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 172 static_assert(B('x').b == 4, ""); 173 static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 174 static_assert(B(123).c == 3, ""); 175 static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 176 static_assert(B("").b == 2, ""); 177 static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 178 179 struct C { 180 union { int a, b = 2, c; }; 181 union { int d, e = 5, f; }; Cdr1460::Overriding::C182 constexpr C() : a(1) {} Cdr1460::Overriding::C183 constexpr C(char) : c(3) {} Cdr1460::Overriding::C184 constexpr C(int) : d(4) {} Cdr1460::Overriding::C185 constexpr C(float) : f(6) {} Cdr1460::Overriding::C186 constexpr C(const char*) {} 187 }; 188 189 static_assert(C().a == 1, ""); 190 static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 191 static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 192 static_assert(C().e == 5, ""); 193 194 static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 195 static_assert(C('x').c == 3, ""); 196 static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 197 static_assert(C('x').e == 5, ""); 198 199 static_assert(C(1).b == 2, ""); 200 static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 201 static_assert(C(1).d == 4, ""); 202 static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 203 204 static_assert(C(1.f).b == 2, ""); 205 static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 206 static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 207 static_assert(C(1.f).f == 6, ""); 208 209 static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 210 static_assert(C("").b == 2, ""); 211 static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 212 static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 213 static_assert(C("").e == 5, ""); 214 static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 215 216 struct D; 217 extern const D d; 218 struct D { 219 int a; 220 union { 221 int b = const_cast<D&>(d).a = 1; // not evaluated 222 int c; 223 }; Ddr1460::Overriding::D224 constexpr D() : a(0), c(0) {} 225 }; 226 constexpr D d {}; 227 static_assert(d.a == 0, ""); 228 } 229 #endif 230 } 231 232 #if __cplusplus >= 201103L 233 namespace std { 234 typedef decltype(sizeof(int)) size_t; 235 236 // libc++'s implementation 237 template <class _E> 238 class initializer_list 239 { 240 const _E* __begin_; 241 size_t __size_; 242 initializer_list(const _E * __b,size_t __s)243 initializer_list(const _E* __b, size_t __s) 244 : __begin_(__b), __size_(__s) {} 245 246 public: 247 typedef _E value_type; 248 typedef const _E& reference; 249 typedef const _E& const_reference; 250 typedef size_t size_type; 251 252 typedef const _E* iterator; 253 typedef const _E* const_iterator; 254 initializer_list()255 initializer_list() : __begin_(nullptr), __size_(0) {} 256 size() const257 size_t size() const {return __size_;} begin() const258 const _E* begin() const {return __begin_;} end() const259 const _E* end() const {return __begin_ + __size_;} 260 }; 261 } // std 262 263 namespace dr1467 { // dr1467: 3.7 c++11 264 // List-initialization of aggregate from same-type object 265 266 namespace basic0 { 267 struct S { 268 int i = 42; 269 }; 270 271 S a; 272 S b(a); 273 S c{a}; 274 275 struct SS : public S { } x; 276 S y(x); 277 S z{x}; 278 } // basic0 279 280 namespace basic1 { 281 struct S { 282 int i{42}; 283 }; 284 285 S a; 286 S b(a); 287 S c{a}; 288 289 struct SS : public S { } x; 290 S y(x); 291 S z{x}; 292 } // basic1 293 294 namespace basic2 { 295 struct S { 296 int i = {42}; 297 }; 298 299 S a; 300 S b(a); 301 S c{a}; 302 303 struct SS : public S { } x; 304 S y(x); 305 S z{x}; 306 } // basic2 307 308 namespace dr_example { 309 struct OK { 310 OK() = default; 311 OK(const OK&) = default; OKdr1467::dr_example::OK312 OK(int) { } 313 }; 314 315 OK ok; 316 OK ok2{ok}; 317 318 struct X { 319 X() = default; 320 X(const X&) = default; 321 }; 322 323 X x; 324 X x2{x}; 325 } // dr_example 326 327 namespace nonaggregate { 328 struct NonAggregate { NonAggregatedr1467::nonaggregate::NonAggregate329 NonAggregate() {} 330 }; 331 332 struct WantsIt { 333 WantsIt(NonAggregate); 334 }; 335 336 void f(NonAggregate); 337 void f(WantsIt); 338 test1()339 void test1() { 340 NonAggregate n; 341 f({n}); 342 } 343 test2()344 void test2() { 345 NonAggregate x; 346 NonAggregate y{x}; 347 NonAggregate z{{x}}; 348 } 349 } // nonaggregate 350 351 namespace SelfInitIsNotListInit { 352 struct S { 353 S(); 354 explicit S(S &); 355 S(const S &); 356 }; 357 S s1; 358 S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor 359 } 360 361 struct NestedInit { int a, b, c; }; 362 NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; 363 364 namespace NestedInit2 { 365 struct Pair { int a, b; }; 366 struct TwoPairs { TwoPairs(Pair, Pair); }; 367 struct Value { Value(Pair); Value(TwoPairs); }; f()368 void f() { Value{{{1,2},{3,4}}}; } 369 } 370 } // dr1467 371 372 namespace dr1490 { // dr1490: 3.7 c++11 373 // List-initialization from a string literal 374 375 char s[4]{"abc"}; // Ok 376 std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}} 377 } // dr190 378 379 namespace dr1495 { // dr1495: 4 380 // Deduction succeeds in both directions. 381 template<typename T, typename U> struct A {}; // expected-note {{template is declared here}} 382 template<typename T, typename U> struct A<U, T> {}; // expected-error {{class template partial specialization is not more specialized}} 383 384 // Primary template is more specialized. 385 template<typename, typename...> struct B {}; // expected-note {{template is declared here}} 386 template<typename ...Ts> struct B<Ts...> {}; // expected-error {{not more specialized}} 387 388 // Deduction fails in both directions. 389 template<int, typename, typename ...> struct C {}; // expected-note {{template is declared here}} 390 template<typename ...Ts> struct C<0, Ts...> {}; // expected-error {{not more specialized}} 391 392 #if __cplusplus >= 201402L 393 // Deduction succeeds in both directions. 394 template<typename T, typename U> int a; // expected-note {{template is declared here}} 395 template<typename T, typename U> int a<U, T>; // expected-error {{variable template partial specialization is not more specialized}} 396 397 // Primary template is more specialized. 398 template<typename, typename...> int b; // expected-note {{template is declared here}} 399 template<typename ...Ts> int b<Ts...>; // expected-error {{not more specialized}} 400 401 // Deduction fails in both directions. 402 template<int, typename, typename ...> int c; // expected-note {{template is declared here}} 403 template<typename ...Ts> int c<0, Ts...>; // expected-error {{not more specialized}} 404 #endif 405 } 406 #endif 407