1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,cxx98 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,cxx11-17,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,cxx98-14,cxx11-17,since-cxx11,since-cxx14 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,cxx11-17,since-cxx11,since-cxx14,cxx17 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,since-cxx20,since-cxx11,since-cxx14 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,since-cxx20,since-cxx23,since-cxx11,since-cxx14 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,since-cxx20,since-cxx23,since-cxx11,since-cxx14 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
9 #if __cplusplus == 199711L
10 #define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)
11 // cxx98-error@-1 {{variadic macros are a C99 feature}}
14 namespace cwg1800
{ // cwg1800: 2.9
15 struct A
{ union { int n
; }; };
16 static_assert(__is_same(__decltype(&A::n
), int A::*), "");
17 } // namespace cwg1800
19 namespace cwg1801
{ // cwg1801: 2.8
24 template <int &> struct S
{}; // #cwg1801-S
25 S
<i
> V
; // #cwg1801-S-i
26 // cxx98-14-error@-1 {{non-type template argument does not refer to any declaration}}
27 // cxx98-14-note@#cwg1801-S {{template parameter is declared here}}
28 // cxx17-error@#cwg1801-S-i {{non-type template argument refers to subobject '.i'}}
31 namespace cwg1802
{ // cwg1802: 3.1
32 #if __cplusplus >= 201103L
33 // Using a Wikipedia example of surrogate pair:
34 // https://en.wikipedia.org/wiki/UTF-16#Examples
35 constexpr char16_t a
[3] = u
"\U00010437";
36 static_assert(a
[0] == 0xD801, "");
37 static_assert(a
[1] == 0xDC37, "");
38 static_assert(a
[2] == 0x0, "");
40 } // namespace cwg1802
42 namespace cwg1803
{ // cwg1803: 2.9
43 #if __cplusplus >= 201103L
53 } // namespace cwg1803
55 namespace cwg1804
{ // cwg1804: 2.7
56 template <typename
, typename
>
93 template <typename
, typename
>
101 template <typename V
>
114 template <typename U
>
115 void A
<int, U
>::f1() {
120 template <typename U
>
121 void A
<double, U
>::f1() {
126 template <typename U
>
127 template <typename V
>
128 void A
<int, U
>::f2(V
) {
133 template <typename U
>
134 template <typename V
>
135 void A
<double, U
>::f2(V
) {
140 template <typename U
>
141 void A
<int, U
>::B::f3() {
146 template <typename U
>
147 void A
<double, U
>::B::f3() {
152 template <typename U
>
153 template <typename V
>
154 void A
<int, U
>::C
<V
>::f4() {
159 template <typename U
>
160 template <typename V
>
161 void A
<double, U
>::C
<V
>::f4() {
165 } // namespace cwg1804
167 // cwg1807 is in cwg1807.cpp
169 namespace cwg1812
{ // cwg1812: no
171 #if __cplusplus >= 201103L
172 template <typename T
> struct A
{
173 using B
= typename
T::C
<int>;
174 // since-cxx11-error@-1 {{use 'template' keyword to treat 'C' as a dependent template name}}
177 } // namespace cwg1812
179 namespace cwg1813
{ // cwg1813: 7
183 struct E
: D
{ char : 4; };
185 static_assert(__is_standard_layout(B
), "");
186 static_assert(__is_standard_layout(C
), "");
187 static_assert(__is_standard_layout(D
), "");
188 static_assert(!__is_standard_layout(E
), "");
195 static_assert(__is_standard_layout(Q
), "");
196 static_assert(__is_standard_layout(S
), "");
197 static_assert(__is_standard_layout(T
), "");
198 static_assert(!__is_standard_layout(U
), "");
201 namespace cwg1814
{ // cwg1814: yes
202 #if __cplusplus >= 201103L
204 auto lam
= [](int x
= 42) { return x
; };
209 namespace cwg1815
{ // cwg1815: 20
210 #if __cplusplus >= 201402L
211 struct A
{ int &&r
= 0; };
214 struct B
{ int &&r
= 0; }; // #cwg1815-B
215 // since-cxx14-error@-1 {{reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
216 // since-cxx14-note@#cwg1815-B {{initializing field 'r' with default member initializer}}
217 // since-cxx14-note@#cwg1815-b {{in implicit default constructor for 'cwg1815::B' first required here}}
220 #if __cplusplus >= 201703L
221 struct C
{ const int &r
= 0; };
222 constexpr C c
= {}; // OK, since cwg1815
223 static_assert(c
.r
== 0);
226 A a
= {}; // OK, since cwg1815
229 static_assert(f() == 0);
234 // cwg1818 is in cwg1818.cpp
236 namespace cwg1820
{ // cwg1820: 3.5
238 typedef int cwg1820::A
;
239 // expected-warning@-1 {{extra qualification on member 'A'}}
240 // expected-error@-2 {{typedef declarator cannot be qualified}}
243 typedef int cwg1820::A
;
244 // expected-error@-1 {{cannot define or redeclare 'A' here because namespace 'B' does not enclose namespace 'cwg1820'}}
245 // expected-error@-2 {{typedef declarator cannot be qualified}}
249 typedef int cwg1820::A
;
250 // expected-error@-1 {{non-friend class member 'A' cannot have a qualified name}}
251 // expected-error@-2 {{typedef declarator cannot be qualified}}
256 typedef int cwg1820::A
;
257 // expected-error@-1 {{non-friend class member 'A' cannot have a qualified name}}
258 // expected-error@-2 {{typedef declarator cannot be qualified}}
262 typedef int cwg1820::A
;
263 // expected-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}
264 // expected-error@-2 {{typedef declarator cannot be qualified}}
269 typedef int cwg1820::A
;
270 // expected-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}
271 // expected-error@-2 {{typedef declarator cannot be qualified}}
274 #if __cplusplus >= 201103L
276 typedef int cwg1820::A
;
277 // expected-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}
278 // expected-error@-2 {{typedef declarator cannot be qualified}}
281 } // namespace cwg1820
283 namespace cwg1821
{ // cwg1821: 2.9
285 template <typename
> struct B
{
288 template <typename T
> void B
<T
>::f(){};
289 // expected-error@-1 {{non-friend class member 'f' cannot have a qualified name}}
295 // expected-error@-1 {{non-friend class member 'f' cannot have a qualified name}}
297 } // namespace cwg1821
299 namespace cwg1822
{ // cwg1822: yes
300 #if __cplusplus >= 201103L
302 auto x
= [] (int a
) {
303 static_assert(__is_same(decltype(a
), int), "should be resolved to lambda parameter");
308 namespace cwg1824
{ // cwg1824: 2.7
315 A
<S
> f() { return A
<S
>(); }
317 } // namespace cwg1824
319 namespace cwg1832
{ // cwg1832: 3.0
320 enum E
{ // #cwg1832-E
321 a
= static_cast<int>(static_cast<E
>(0))
322 // expected-error@-1 {{'E' is an incomplete type}}
323 // expected-note@#cwg1832-E {{definition of 'cwg1832::E' is not complete until the closing '}'}}
326 #if __cplusplus >= 201103L
327 enum E2
: decltype(static_cast<E2
>(0), 0) {};
328 // expected-error@-1 {{unknown type name 'E2'}}
329 enum class E3
: decltype(static_cast<E3
>(0), 0) {};
330 // expected-error@-1 {{unknown type name 'E3'}}
332 } // namespace cwg1832
334 namespace cwg1837
{ // cwg1837: 3.3
335 #if __cplusplus >= 201103L
336 template <typename T
>
337 struct Fish
{ static const bool value
= true; };
341 auto q() -> decltype(p()) *;
345 friend auto Other::q() -> decltype(this->p()) *;
346 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
349 extern void f(decltype(this->g()) *);
351 static_assert(Fish
<decltype(this->g())>::value
, "");
352 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
353 enum { X
= Fish
<decltype(this->f())>::value
};
354 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
355 struct Inner2
: Fish
<decltype(this->g())> { };
356 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
357 friend void f(decltype(this->g()) *);
358 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
359 friend auto Other::q() -> decltype(this->p()) *;
360 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}
369 // since-cxx11-warning@-1 {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
371 static_assert(sizeof(this->f()) == sizeof(int), "");
378 namespace cwg1862
{ // cwg1862: no
422 friend struct A
<T
>::B
;
423 // expected-warning@-1 {{dependent nested name specifier 'A<T>::' for friend class declaration is not supported; turning off access control for 'C'}}
426 friend void A
<T
>::f();
427 // expected-warning@-1 {{dependent nested name specifier 'A<T>::' for friend class declaration is not supported; turning off access control for 'C'}}
429 // FIXME: this is ill-formed, because A<T>​::​D does not end with a simple-template-id
431 friend void A
<T
>::D::g();
432 // expected-warning@-1 {{dependent nested name specifier 'A<T>::D::' for friend class declaration is not supported; turning off access control for 'C'}}
435 friend int *A
<T
*>::h();
436 // expected-warning@-1 {{dependent nested name specifier 'A<T *>::' for friend class declaration is not supported; turning off access control for 'C'}}
441 // expected-warning@-1 {{dependent nested name specifier 'A<T>::' for friend class declaration is not supported; turning off access control for 'C'}}
447 void A
<T
>::B::e() { (void)c
.private_int
; }
448 void A
<int>::B::e() { (void)c
.private_int
; }
451 void A
<T
>::f() { (void)c
.private_int
; }
452 int A
<int>::f() { (void)c
.private_int
; return 0; }
454 // FIXME: both definition of 'D::g' are not friends, so they don't have access to 'private_int'
456 void A
<T
>::D::g() { (void)c
.private_int
; }
457 void A
<int>::D::g() { (void)c
.private_int
; }
460 T A
<T
>::h() { (void)c
.private_int
; }
461 int* A
<float*>::h() { (void)c
.private_int
; return 0; }
465 T A
<T
>::i() { (void)c
.private_int
; }
467 int A
<int>::i() { (void)c
.private_int
; }
468 } // namespace cwg1862
470 namespace cwg1872
{ // cwg1872: 9
471 #if __cplusplus >= 201103L
472 template<typename T
> struct A
: T
{
473 constexpr int f() const { return 0; }
476 struct Y
{ virtual int f() const; };
477 struct Z
: virtual X
{};
479 constexpr int x
= A
<X
>().f();
480 constexpr int y
= A
<Y
>().f();
481 // cxx11-17-error@-1 {{constexpr variable 'y' must be initialized by a constant expression}}
482 // cxx11-17-note@-2 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}}
483 #if __cplusplus >= 202002L
484 static_assert(y
== 0);
486 // Note, this is invalid even though it would not use virtual dispatch.
487 constexpr int y2
= A
<Y
>().A
<Y
>::f();
488 // cxx11-17-error@-1 {{constexpr variable 'y2' must be initialized by a constant expression}}
489 // cxx11-17-note@-2 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}}
490 #if __cplusplus >= 202002L
491 static_assert(y
== 0);
493 constexpr int z
= A
<Z
>().f();
494 // since-cxx11-error@-1 {{constexpr variable 'z' must be initialized by a constant expression}}a
495 #if __cplusplus < 202302L
496 // since-cxx11-note@-3 {{non-literal type 'A<Z>' cannot be used in a constant expression}}
498 // since-cxx23-note@-5 {{cannot construct object of type 'A<cwg1872::Z>' with virtual base class in a constant expression}}
503 namespace cwg1878
{ // cwg1878: 18
504 #if __cplusplus >= 201402L
505 #if __cplusplus >= 202002L
506 template <typename T
>
511 template <typename T
>
512 operator auto() const { return short(); }
513 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}
514 template <typename T
>
515 operator const auto() const { return int(); }
516 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}
517 template <typename T
>
518 operator const auto&() const { return char(); }
519 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}
520 template <typename T
>
521 operator const auto*() const { return long(); }
522 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}
523 template <typename T
>
524 operator decltype(auto)() const { return unsigned(); }
525 // since-cxx14-error@-1 {{'decltype(auto)' not allowed in declaration of conversion function template}}
526 #if __cplusplus >= 202002L
527 template <typename T
>
528 operator C
auto() const { return float(); }
529 // since-cxx20-error@-1 {{'auto' not allowed in declaration of conversion function template}}
530 template <typename T
>
531 operator C
decltype(auto)() const { return double(); }
532 // since-cxx20-error@-1 {{'decltype(auto)' not allowed in declaration of conversion function template}}
538 namespace cwg1881
{ // cwg1881: 7
539 struct A
{ int a
: 4; };
540 struct B
: A
{ int b
: 3; };
541 static_assert(__is_standard_layout(A
), "");
542 static_assert(!__is_standard_layout(B
), "");
544 struct C
{ int : 0; };
545 struct D
: C
{ int : 0; };
546 static_assert(__is_standard_layout(C
), "");
547 static_assert(!__is_standard_layout(D
), "");
550 // cwg1884 is in cwg1884.cpp
552 namespace cwg1890
{ // cwg1890: no drafting 2018-06-04
553 // FIXME: current consensus for CWG2335 is that the examples are well-formed.
555 #if __cplusplus >= 201402L
558 auto foo() { return 0; } // #cwg1890-foo
560 decltype(B().foo()) x
;
561 // since-cxx14-error@-1 {{function 'foo' with deduced return type cannot be used before it is defined}}
562 // since-cxx14-note@#cwg1890-foo {{'foo' declared here}}
568 #if __cplusplus >= 201103L
573 static_assert(__is_constructible(Baz
), "");
574 // since-cxx11-error@-1 {{static assertion failed due to requirement '__is_constructible(cwg1890::ex2::Bar::Baz)'}}
578 } // namespace cwg1890
580 void cwg1891() { // cwg1891: 4
581 #if __cplusplus >= 201103L
583 auto a
= []{}; // #cwg1891-a
584 auto b
= [=]{ return n
; }; // #cwg1891-b
585 typedef decltype(a
) A
;
586 typedef decltype(b
) B
;
588 static_assert(!__has_trivial_constructor(A
), "");
589 // since-cxx20-error@-1 {{failed}}
590 static_assert(!__has_trivial_constructor(B
), "");
592 // C++20 allows default construction for non-capturing lambdas (P0624R2).
594 // cxx11-17-error@-1 {{no matching constructor for initialization of 'A' (aka '(lambda at}}
595 // cxx11-17-note@#cwg1891-a {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}
596 // cxx11-17-note@#cwg1891-a {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}
598 // since-cxx11-error@-1 {{no matching constructor for initialization of 'B' (aka '(lambda at}}
599 // since-cxx11-note@#cwg1891-b {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}
600 // since-cxx11-note@#cwg1891-b {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}
602 // C++20 allows assignment for non-capturing lambdas (P0624R2).
604 // cxx11-17-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}
605 // cxx11-17-note@#cwg1891-a {{lambda expression begins here}}
606 a
= static_cast<A
&&>(a
);
607 // cxx11-17-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}
608 // cxx11-17-note@#cwg1891-a {{lambda expression begins here}}
610 // since-cxx11-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}
611 // since-cxx11-note@#cwg1891-b {{lambda expression begins here}}
612 b
= static_cast<B
&&>(b
);
613 // since-cxx11-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}
614 // since-cxx11-note@#cwg1891-b {{lambda expression begins here}}
618 namespace cwg1894
{ // cwg1894: 3.8
619 // NB: reusing part of cwg407 test
646 namespace cwg1898
{ // cwg1898: 2.7
647 void e(int) {} // #cwg1898-e
649 // expected-error@-1 {{redefinition of 'e'}}
650 // expected-note@#cwg1898-e {{previous definition is here}}
653 void e2(long) {} // OK, different type
655 void f(int) {} // #cwg1898-f
657 // expected-error@-1 {{redefinition of 'f'}}
658 // expected-note@#cwg1898-f {{previous definition is here}}
660 void g(int) {} // #cwg1898-g
661 void g(volatile int) {}
662 // since-cxx20-warning@-1 {{volatile-qualified parameter type 'volatile int' is deprecated}}
663 // expected-error@-2 {{redefinition of 'g'}}
664 // expected-note@#cwg1898-g {{previous definition is here}}
666 void h(int *) {} // #cwg1898-h
668 // expected-error@-1 {{redefinition of 'h'}}
669 // expected-note@#cwg1898-h {{previous definition is here}}
671 void h2(int *) {} // #cwg1898-h2
673 // expected-error@-1 {{redefinition of 'h2'}}
674 // expected-note@#cwg1898-h2 {{previous definition is here}}
676 void h3(int (*)[2]) {} // #cwg1898-h3
677 void h3(int [3][2]) {}
678 // expected-error@-1 {{redefinition of 'h3'}}
679 // expected-note@#cwg1898-h3 {{previous definition is here}}
681 void h4(int (*)[2]) {}
682 void h4(int [3][3]) {} // OK, differ in non-top-level extent of array
685 void i(const int *) {} // OK, pointee cv-qualification is not discarded
687 void i2(int *) {} // #cwg1898-i2
688 void i2(int * const) {}
689 // expected-error@-1 {{redefinition of 'i2'}}
690 // expected-note@#cwg1898-i2 {{previous definition is here}}
692 void j(void(*)()) {} // #cwg1898-j
694 // expected-error@-1 {{redefinition of 'j'}}
695 // expected-note@#cwg1898-j {{previous definition is here}}
697 void j2(void(int)) {} // #cwg1898-j2
698 void j2(void(const int)) {}
699 // expected-error@-1 {{redefinition of 'j2'}}
700 // expected-note@#cwg1898-j2 {{previous definition is here}}
703 void k(int) {} // #cwg1898-k
705 // expected-error@-1 {{class member cannot be redeclared}}
706 // expected-note@#cwg1898-k {{previous definition is here}}
710 void k(int) {} // OK, shadows A::k
716 #if __cplusplus >= 201103L
717 template <typename T
>
719 template <typename
... Ts
>
722 template <typename T
, typename U
>
724 template <typename
... Ts
, typename U
>
727 } // namespace cwg1898