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 namespace dr1413 { // dr1413: 12 8 template<int> struct Check { 9 typedef int type; 10 }; 11 template<typename T> struct A : T { 12 static const int a = 1; 13 static const int b; 14 static void c(); 15 void d(); 16 fdr1413::A17 void f() { 18 Check<true ? 0 : A::unknown_spec>::type *var1; // expected-error {{undeclared identifier 'var1'}} 19 Check<true ? 0 : a>::type *var2; // ok, variable declaration expected-note 0+{{here}} 20 Check<true ? 0 : b>::type *var3; // expected-error {{undeclared identifier 'var3'}} 21 Check<true ? 0 : ((void)c, 0)>::type *var4; // expected-error {{undeclared identifier 'var4'}} 22 // value-dependent because of the implied type-dependent 'this->', not because of 'd' 23 Check<true ? 0 : (d(), 0)>::type *var5; // expected-error {{undeclared identifier 'var5'}} 24 // value-dependent because of the value-dependent '&' operator, not because of 'A::d' 25 Check<true ? 0 : (&A::d(), 0)>::type *var5; // expected-error {{undeclared identifier 'var5'}} 26 } 27 }; 28 } 29 30 namespace dr1423 { // dr1423: 11 31 #if __cplusplus >= 201103L 32 bool b1 = nullptr; // expected-error {{cannot initialize}} 33 bool b2(nullptr); // expected-warning {{implicit conversion of nullptr constant to 'bool'}} 34 bool b3 = {nullptr}; // expected-error {{cannot initialize}} 35 bool b4{nullptr}; // expected-warning {{implicit conversion of nullptr constant to 'bool'}} 36 #endif 37 } 38 39 namespace dr1443 { // dr1443: yes 40 struct A { 41 int i; Adr1443::A42 A() { void foo(int=i); } // expected-error {{default argument references 'this'}} 43 }; 44 } 45 46 // dr1425: na abi 47 48 namespace dr1460 { // dr1460: 3.5 49 #if __cplusplus >= 201103L 50 namespace DRExample { 51 union A { 52 union {}; // expected-error {{does not declare anything}} 53 union {}; // expected-error {{does not declare anything}} A()54 constexpr A() {} 55 }; 56 constexpr A a = A(); 57 58 union B { 59 union {}; // expected-error {{does not declare anything}} 60 union {}; // expected-error {{does not declare anything}} 61 constexpr B() = default; 62 }; 63 constexpr B b = B(); 64 65 union C { 66 union {}; // expected-error {{does not declare anything}} 67 union {}; // expected-error {{does not declare anything}} 68 }; 69 constexpr C c = C(); 70 #if __cplusplus > 201103L f()71 constexpr void f() { C c; } 72 static_assert((f(), true), ""); 73 #endif 74 } 75 76 union A {}; 77 union B { int n; }; // expected-note 0+{{here}} 78 union C { int n = 0; }; 79 struct D { union {}; }; // expected-error {{does not declare anything}} 80 struct E { union { int n; }; }; // expected-note 0+{{here}} 81 struct F { union { int n = 0; }; }; 82 83 struct X { 84 friend constexpr A::A() noexcept; 85 friend constexpr B::B() noexcept; 86 #if __cplusplus <= 201703L 87 // expected-error@-2 {{follows non-constexpr declaration}} 88 #endif 89 friend constexpr C::C() noexcept; 90 friend constexpr D::D() noexcept; 91 friend constexpr E::E() noexcept; 92 #if __cplusplus <= 201703L 93 // expected-error@-2 {{follows non-constexpr declaration}} 94 #endif 95 friend constexpr F::F() noexcept; 96 }; 97 98 // These are OK, because value-initialization doesn't actually invoke the 99 // constructor. 100 constexpr A a = A(); 101 constexpr B b = B(); 102 constexpr C c = C(); 103 constexpr D d = D(); 104 constexpr E e = E(); 105 constexpr F f = F(); 106 107 namespace Defaulted { 108 union A { constexpr A() = default; }; 109 union B { int n; constexpr B() = default; }; 110 #if __cplusplus <= 201703L 111 // expected-error@-2 {{not constexpr}} 112 #endif 113 union C { int n = 0; constexpr C() = default; }; 114 struct D { union {}; constexpr D() = default; }; // expected-error {{does not declare anything}} 115 struct E { union { int n; }; constexpr E() = default; }; 116 #if __cplusplus <= 201703L 117 // expected-error@-2 {{not constexpr}} 118 #endif 119 struct F { union { int n = 0; }; constexpr F() = default; }; 120 121 struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; 122 #if __cplusplus <= 201703L 123 // expected-error@-2 {{not constexpr}} 124 #endif 125 struct H { 126 union { 127 int n = 0; 128 }; 129 union { // expected-note 0-2{{member not initialized}} 130 int m; 131 }; Hdr1460::Defaulted::H132 constexpr H() {} 133 #if __cplusplus <= 201703L 134 // expected-error@-2 {{initialize all members}} 135 #endif Hdr1460::Defaulted::H136 constexpr H(bool) : m(1) {} Hdr1460::Defaulted::H137 constexpr H(char) : n(1) {} 138 #if __cplusplus <= 201703L 139 // expected-error@-2 {{initialize all members}} 140 #endif Hdr1460::Defaulted::H141 constexpr H(double) : m(1), n(1) {} 142 }; 143 } 144 145 #if __cplusplus > 201103L check()146 template<typename T> constexpr bool check() { 147 T t; 148 #if __cplusplus <= 201703L 149 // expected-note-re@-2 2{{non-constexpr constructor '{{[BE]}}'}} 150 #endif 151 return true; 152 } 153 static_assert(check<A>(), ""); 154 static_assert(check<B>(), ""); 155 #if __cplusplus <= 201703L 156 // expected-error@-2 {{constant}} expected-note@-2 {{in call}} 157 #endif 158 static_assert(check<C>(), ""); 159 static_assert(check<D>(), ""); 160 static_assert(check<E>(), ""); 161 #if __cplusplus <= 201703L 162 // expected-error@-2 {{constant}} expected-note@-2 {{in call}} 163 #endif 164 static_assert(check<F>(), ""); 165 #endif 166 167 union G { 168 int a = 0; // expected-note {{previous initialization is here}} 169 int b = 0; // expected-error {{initializing multiple members of union}} 170 }; 171 union H { 172 union { 173 int a = 0; // expected-note {{previous initialization is here}} 174 }; 175 union { 176 int b = 0; // expected-error {{initializing multiple members of union}} 177 }; 178 }; 179 struct I { 180 union { 181 int a = 0; // expected-note {{previous initialization is here}} 182 int b = 0; // expected-error {{initializing multiple members of union}} 183 }; 184 }; 185 struct J { 186 union { int a = 0; }; 187 union { int b = 0; }; 188 }; 189 190 namespace Overriding { 191 struct A { 192 int a = 1, b, c = 3; Adr1460::Overriding::A193 constexpr A() : b(2) {} 194 }; 195 static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 196 197 union B { 198 int a, b = 2, c; B()199 constexpr B() : a(1) {} B(char)200 constexpr B(char) : b(4) {} B(int)201 constexpr B(int) : c(3) {} B(const char *)202 constexpr B(const char*) {} 203 }; 204 static_assert(B().a == 1, ""); 205 static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 206 static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 207 static_assert(B('x').b == 4, ""); 208 static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 209 static_assert(B(123).c == 3, ""); 210 static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 211 static_assert(B("").b == 2, ""); 212 static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 213 214 struct C { 215 union { int a, b = 2, c; }; 216 union { int d, e = 5, f; }; Cdr1460::Overriding::C217 constexpr C() : a(1) {} Cdr1460::Overriding::C218 constexpr C(char) : c(3) {} Cdr1460::Overriding::C219 constexpr C(int) : d(4) {} Cdr1460::Overriding::C220 constexpr C(float) : f(6) {} Cdr1460::Overriding::C221 constexpr C(const char*) {} 222 }; 223 224 static_assert(C().a == 1, ""); 225 static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 226 static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 227 static_assert(C().e == 5, ""); 228 229 static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 230 static_assert(C('x').c == 3, ""); 231 static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 232 static_assert(C('x').e == 5, ""); 233 234 static_assert(C(1).b == 2, ""); 235 static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 236 static_assert(C(1).d == 4, ""); 237 static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 238 239 static_assert(C(1.f).b == 2, ""); 240 static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 241 static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 242 static_assert(C(1.f).f == 6, ""); 243 244 static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 245 static_assert(C("").b == 2, ""); 246 static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 247 static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 248 static_assert(C("").e == 5, ""); 249 static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 250 251 struct D; 252 extern const D d; 253 struct D { 254 int a; 255 union { 256 int b = const_cast<D&>(d).a = 1; // not evaluated 257 int c; 258 }; Ddr1460::Overriding::D259 constexpr D() : a(0), c(0) {} 260 }; 261 constexpr D d {}; 262 static_assert(d.a == 0, ""); 263 } 264 #endif 265 } 266 267 #if __cplusplus >= 201103L 268 namespace std { 269 typedef decltype(sizeof(int)) size_t; 270 271 // libc++'s implementation 272 template <class _E> 273 class initializer_list 274 { 275 const _E* __begin_; 276 size_t __size_; 277 initializer_list(const _E * __b,size_t __s)278 initializer_list(const _E* __b, size_t __s) 279 : __begin_(__b), __size_(__s) {} 280 281 public: 282 typedef _E value_type; 283 typedef const _E& reference; 284 typedef const _E& const_reference; 285 typedef size_t size_type; 286 287 typedef const _E* iterator; 288 typedef const _E* const_iterator; 289 initializer_list()290 initializer_list() : __begin_(nullptr), __size_(0) {} 291 size() const292 size_t size() const {return __size_;} begin() const293 const _E* begin() const {return __begin_;} end() const294 const _E* end() const {return __begin_ + __size_;} 295 }; 296 } // std 297 298 namespace dr1467 { // dr1467: 3.7 c++11 299 // Note that the change to [over.best.ics] was partially undone by DR2076; 300 // the resulting rule is tested with the tests for that change. 301 302 // List-initialization of aggregate from same-type object 303 304 namespace basic0 { 305 struct S { 306 int i = 42; 307 }; 308 309 S a; 310 S b(a); 311 S c{a}; 312 313 struct SS : public S { } x; 314 S y(x); 315 S z{x}; 316 } // basic0 317 318 namespace basic1 { 319 struct S { 320 int i{42}; 321 }; 322 323 S a; 324 S b(a); 325 S c{a}; 326 327 struct SS : public S { } x; 328 S y(x); 329 S z{x}; 330 } // basic1 331 332 namespace basic2 { 333 struct S { 334 int i = {42}; 335 }; 336 337 S a; 338 S b(a); 339 S c{a}; 340 341 struct SS : public S { } x; 342 S y(x); 343 S z{x}; 344 } // basic2 345 346 namespace dr_example { 347 struct OK { 348 OK() = default; 349 OK(const OK&) = default; OKdr1467::dr_example::OK350 OK(int) { } 351 }; 352 353 OK ok; 354 OK ok2{ok}; 355 356 struct X { 357 X() = default; 358 X(const X&) = default; 359 }; 360 361 X x; 362 X x2{x}; 363 364 void f1(int); // expected-note {{candidate function}} 365 void f1(std::initializer_list<long>) = delete; // expected-note {{candidate function has been explicitly deleted}} g1()366 void g1() { f1({42}); } // expected-error {{call to deleted function 'f1'}} 367 368 template <class T, class U> 369 struct Pair { 370 Pair(T, U); 371 }; 372 struct String { 373 String(const char *); 374 }; 375 376 void f2(Pair<const char *, const char *>); // expected-note {{candidate function}} 377 void f2(std::initializer_list<String>) = delete; // expected-note {{candidate function has been explicitly deleted}} g2()378 void g2() { f2({"foo", "bar"}); } // expected-error {{call to deleted function 'f2'}} 379 } // dr_example 380 381 namespace nonaggregate { 382 struct NonAggregate { NonAggregatedr1467::nonaggregate::NonAggregate383 NonAggregate() {} 384 }; 385 386 struct WantsIt { 387 WantsIt(NonAggregate); 388 }; 389 390 void f(NonAggregate); 391 void f(WantsIt); 392 test1()393 void test1() { 394 NonAggregate n; 395 f({n}); 396 } 397 test2()398 void test2() { 399 NonAggregate x; 400 NonAggregate y{x}; 401 NonAggregate z{{x}}; 402 } 403 } // nonaggregate 404 405 namespace SelfInitIsNotListInit { 406 struct S { 407 S(); 408 explicit S(S &); 409 S(const S &); 410 }; 411 S s1; 412 S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor 413 } 414 415 struct NestedInit { int a, b, c; }; 416 NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; 417 418 namespace NestedInit2 { 419 struct Pair { int a, b; }; 420 struct TwoPairs { TwoPairs(Pair, Pair); }; 421 struct Value { Value(Pair); Value(TwoPairs); }; f()422 void f() { Value{{{1,2},{3,4}}}; } 423 } 424 namespace NonAmbiguous { 425 // The original implementation made this case ambiguous due to the special 426 // handling of one element initialization lists. 427 void f(int(&&)[1]); 428 void f(unsigned(&&)[1]); 429 g(unsigned i)430 void g(unsigned i) { 431 f({i}); 432 } 433 } // namespace NonAmbiguous 434 435 #if __cplusplus >= 201103L 436 namespace StringLiterals { 437 // When the array size is 4 the call will attempt to bind an lvalue to an 438 // rvalue and fail. Therefore #2 will be called. (rsmith will bring this 439 // issue to CWG) 440 void f(const char(&&)[4]); // expected-note 2 {{expects an rvalue}} expected-note 3 {{no known conversion}} 441 void f(const char(&&)[5]) = delete; // expected-note 2 {{candidate function has been explicitly deleted}} expected-note 3 {{no known conversion}} 442 void f(const wchar_t(&&)[4]); // expected-note {{expects an rvalue}} expected-note 4 {{no known conversion}} 443 void f(const wchar_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}} 444 #if __cplusplus >= 202002L 445 void f2(const char8_t(&&)[4]); // expected-note {{expects an rvalue}} 446 void f2(const char8_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} 447 #endif 448 void f(const char16_t(&&)[4]); // expected-note {{expects an rvalue}} expected-note 4 {{no known conversion}} 449 void f(const char16_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}} 450 void f(const char32_t(&&)[4]); // expected-note {{expects an rvalue}} expected-note 4 {{no known conversion}} 451 void f(const char32_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}} g()452 void g() { 453 f({"abc"}); // expected-error {{call to deleted function 'f'}} 454 f({((("abc")))}); // expected-error {{call to deleted function 'f'}} 455 f({L"abc"}); // expected-error {{call to deleted function 'f'}} 456 #if __cplusplus >= 202002L 457 f2({u8"abc"}); // expected-error {{call to deleted function 'f2'}} 458 #endif 459 f({uR"(abc)"}); // expected-error {{call to deleted function 'f'}} 460 f({(UR"(abc)")}); // expected-error {{call to deleted function 'f'}} 461 } 462 } // namespace StringLiterals 463 #endif 464 } // dr1467 465 466 namespace dr1479 { // dr1479: yes 467 int operator"" _a(const char*, std::size_t = 0); // expected-error {{literal operator cannot have a default argument}} 468 } 469 470 namespace dr1490 { // dr1490: 3.7 c++11 471 // List-initialization from a string literal 472 473 char s[4]{"abc"}; // Ok 474 std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}} 475 } // dr190 476 477 namespace dr1495 { // dr1495: 4 478 // Deduction succeeds in both directions. 479 template<typename T, typename U> struct A {}; // expected-note {{template is declared here}} 480 template<typename T, typename U> struct A<U, T> {}; // expected-error {{class template partial specialization is not more specialized}} 481 482 // Primary template is more specialized. 483 template<typename, typename...> struct B {}; // expected-note {{template is declared here}} 484 template<typename ...Ts> struct B<Ts...> {}; // expected-error {{not more specialized}} 485 486 // Deduction fails in both directions. 487 template<int, typename, typename ...> struct C {}; // expected-note {{template is declared here}} 488 template<typename ...Ts> struct C<0, Ts...> {}; // expected-error {{not more specialized}} 489 490 #if __cplusplus >= 201402L 491 // Deduction succeeds in both directions. 492 template<typename T, typename U> int a; // expected-note {{template is declared here}} 493 template<typename T, typename U> int a<U, T>; // expected-error {{variable template partial specialization is not more specialized}} 494 495 // Primary template is more specialized. 496 template<typename, typename...> int b; // expected-note {{template is declared here}} 497 template<typename ...Ts> int b<Ts...>; // expected-error {{not more specialized}} 498 499 // Deduction fails in both directions. 500 template<int, typename, typename ...> int c; // expected-note {{template is declared here}} 501 template<typename ...Ts> int c<0, Ts...>; // expected-error {{not more specialized}} 502 #endif 503 } 504 505 namespace dr1496 { // dr1496: no 506 #if __cplusplus >= 201103L 507 struct A { 508 A() = delete; 509 }; 510 // FIXME: 'A' should not be trivial because the class lacks at least one 511 // default constructor which is not deleted. 512 static_assert(__is_trivial(A), ""); 513 #endif 514 } 515 516 #endif 517