1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify=expected,both %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s
3 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -triple i686 -verify=expected,both %s
4 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,both %s
5 // RUN: %clang_cc1 -verify=ref,both -std=c++14 %s
6 // RUN: %clang_cc1 -verify=ref,both -std=c++17 %s
7 // RUN: %clang_cc1 -verify=ref,both -std=c++17 -triple i686 %s
8 // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
12 constexpr Empty e
= {Empty()};
23 BoolPair bp
= {true, false};
24 int numbers
[3] = {1,2,3};
26 static const int five
= 5;
27 static constexpr int getFive() {
31 constexpr int getTen() const {
36 static_assert(Ints::getFive() == 5, "");
39 static_assert(ints
.a
== 20, "");
40 static_assert(ints
.b
== 30, "");
41 static_assert(ints
.c
, "");
42 static_assert(ints
.getTen() == 10, "");
43 static_assert(ints
.numbers
[0] == 1, "");
44 static_assert(ints
.numbers
[1] == 2, "");
45 static_assert(ints
.numbers
[2] == 3, "");
47 constexpr const BoolPair
&BP
= ints
.bp
;
48 static_assert(BP
.first
, "");
49 static_assert(!BP
.second
, "");
50 static_assert(ints
.bp
.first
, "");
51 static_assert(!ints
.bp
.second
, "");
54 constexpr Ints ints2
{-20, -30, false};
55 static_assert(ints2
.a
== -20, "");
56 static_assert(ints2
.b
== -30, "");
57 static_assert(!ints2
.c
, "");
59 constexpr Ints
getInts() {
60 return {64, 128, true};
62 constexpr Ints ints3
= getInts();
63 static_assert(ints3
.a
== 64, "");
64 static_assert(ints3
.b
== 128, "");
65 static_assert(ints3
.c
, "");
67 constexpr Ints ints4
= {
73 static_assert(ints4
.a
== (40 * 50), "");
74 static_assert(ints4
.b
== 0, "");
75 static_assert(ints4
.c
, "");
76 static_assert(ints4
.numbers
[0] == 1, "");
77 static_assert(ints4
.numbers
[1] == 2, "");
78 static_assert(ints4
.numbers
[2] == 3, "");
80 constexpr Ints ints5
= ints4
;
81 static_assert(ints5
.a
== (40 * 50), "");
82 static_assert(ints5
.b
== 0, "");
83 static_assert(ints5
.c
, "");
84 static_assert(ints5
.numbers
[0] == 1, "");
85 static_assert(ints5
.numbers
[1] == 2, "");
86 static_assert(ints5
.numbers
[2] == 3, "");
93 constexpr Ints2 ints22
; // both-error {{without a user-provided default constructor}}
95 constexpr Ints2 I2
= Ints2
{12, 25};
96 static_assert(I2
.a
== 12, "");
97 static_assert(I2
.b
== 25, "");
104 constexpr C() : a(100), b(200) {}
106 constexpr C
get() const {
112 static_assert(c
.a
== 100, "");
113 static_assert(c
.b
== 200, "");
115 constexpr C c2
= C().get();
116 static_assert(c2
.a
== 100, "");
117 static_assert(c2
.b
== 200, "");
120 /// A global, composite temporary variable.
121 constexpr const C
&c3
= C().get();
123 /// Same, but with a bitfield.
127 constexpr D() : a(15) {}
128 constexpr D
get() const {
132 constexpr const D
&d4
= D().get();
134 constexpr int getB() {
142 static_assert(getB() == 400, "");
144 constexpr int getA(const C
&c
) {
147 static_assert(getA(c
) == 100, "");
149 constexpr const C
* getPointer() {
152 static_assert(getPointer()->a
== 100, "");
154 constexpr C
RVOAndParams(const C
*c
) {
157 constexpr C RVOAndParamsResult
= RVOAndParams(&c
);
159 /// Parameter and return value have different types.
160 constexpr C
RVOAndParams(int a
) {
163 constexpr C RVOAndParamsResult2
= RVOAndParams(12);
165 class Bar
{ // both-note {{definition of 'Bar' is not complete}}
168 constexpr Bar b
; // both-error {{cannot be constexpr}} \
169 // both-error {{has incomplete type 'const Bar'}}
171 constexpr Bar B
; // both-error {{must be initialized by a constant expression}}
172 constexpr Bar
*pb
= nullptr;
174 constexpr int locals() {
178 // Assignment, not an initializer.
183 // Assignment, not an initializer.
184 c
= RVOAndParams(&c
);
188 static_assert(locals() == 100, "");
190 namespace thisPointer
{
192 constexpr int get12() { return 12; }
195 constexpr int foo() { // both-error {{never produces a constant expression}}
197 return s
->get12(); // both-note 2{{member call on dereferenced null pointer}}
200 static_assert(foo() == 12, ""); // both-error {{not an integral constant expression}} \
201 // both-note {{in call to 'foo()'}}
204 struct FourBoolPairs
{
213 constexpr FourBoolPairs LT
;
215 constexpr FourBoolPairs LT2
= LT
;
216 static_assert(LT2
.v
[0].first
== false, "");
217 static_assert(LT2
.v
[0].second
== false, "");
218 static_assert(LT2
.v
[2].first
== true, "");
219 static_assert(LT2
.v
[2].second
== false, "");
224 constexpr Base() : i(10) {}
225 constexpr Base(int i
) : i(i
) {}
228 class A
: public Base
{
230 constexpr A() : Base(100) {}
231 constexpr A(int a
) : Base(a
) {}
234 static_assert(a
.i
== 100, "");
236 static_assert(a2
.i
== 12, "");
237 static_assert(a2
.i
== 200, ""); // both-error {{static assertion failed}} \
238 // both-note {{evaluates to '12 == 200'}}
243 constexpr int get5() const { return 5; }
244 constexpr void fo() const {
245 this; // both-warning {{expression result unused}}
246 this->a
; // both-warning {{expression result unused}}
251 constexpr int m() const {
257 static_assert(s
.m() == 1, "");
259 namespace InitializerTemporaries
{
265 constexpr Bar() : a(10) {}
266 constexpr int getA() const { return a
; }
273 constexpr Foo() : a(Bar().getA()) {}
276 static_assert(F
.a
== 10, "");
279 /// Needs constexpr destructors.
280 #if __cplusplus >= 202002L
282 /// Arr[Pos] = Value;
284 /// in its destructor.
292 constexpr BitSetter(int *Arr
, int &Pos
, int Value
) :
293 Arr(Arr
), Pos(Pos
), Value(Value
) {}
295 constexpr int getValue() const { return 0; }
296 constexpr ~BitSetter() {
305 constexpr Test(int *Arr
, int &Pos
) :
306 a(BitSetter(Arr
, Pos
, 1).getValue()),
307 b(BitSetter(Arr
, Pos
, 2).getValue()),
308 c(BitSetter(Arr
, Pos
, 3).getValue())
313 constexpr int T(int Index
) {
314 int Arr
[] = {0, 0, 0};
319 // End of scope, should destroy Test.
324 static_assert(T(0) == 1);
325 static_assert(T(1) == 2);
326 static_assert(T(2) == 3);
328 // Invalid destructor.
331 constexpr ~S() noexcept(false) { throw 12; } // both-error {{cannot use 'throw'}} \
332 // both-error {{never produces a constant expression}} \
333 // both-note 2{{subexpression not valid}}
337 S
{}; // both-note {{in call to 'S{}.~S()'}}
340 static_assert(f() == 12); // both-error {{not an integral constant expression}} \
341 // both-note {{in call to 'f()'}}
347 #if __cplusplus >= 201703L
349 class _A
{public: int a
;};
350 class _B
: public _A
{};
351 class _C
: public _B
{};
354 constexpr const _B
&b
= c
;
355 static_assert(b
.a
== 12);
357 class A
{public: int a
;};
358 class B
: public A
{};
359 class C
: public A
{};
360 class D
: public B
, public C
{};
362 // This initializes D::B::A::a and not D::C::A::a.
364 static_assert(d
.B::a
== 12);
365 static_assert(d
.C::a
== 0);
373 constexpr A(int a
) : a(a
) {}
379 constexpr B(int b
) : b(b
) {}
382 class C
: public A
, public B
{
384 constexpr C() : A(10), B(20) {}
387 static_assert(c
.a
== 10, "");
388 static_assert(c
.b
== 20, "");
390 constexpr const A
*aPointer
= &c
;
391 constexpr const B
*bPointer
= &c
;
393 class D
: private A
, private B
{
395 constexpr D() : A(20), B(30) {}
396 constexpr int getA() const { return a
; }
397 constexpr int getB() const { return b
; }
400 static_assert(d
.getA() == 20, "");
401 static_assert(d
.getB() == 30, "");
404 namespace DeriveFailures
{
405 #if __cplusplus < 202002L
406 struct Base
{ // both-note {{declared here}} \
407 // ref-note {{declared here}}
411 struct Derived
: Base
{
414 constexpr Derived(int i
) : OtherVal(i
) {} // ref-error {{never produces a constant expression}} \
415 // both-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}} \
416 // ref-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}}
419 constexpr Derived
D(12); // both-error {{must be initialized by a constant expression}} \
420 // both-note {{in call to 'Derived(12)'}} \
421 // both-note {{declared here}}
423 static_assert(D
.Val
== 0, ""); // both-error {{not an integral constant expression}} \
424 // both-note {{initializer of 'D' is not a constant expression}}
429 constexpr AnotherBase(int i
) : Val(12 / i
) {} // both-note {{division by zero}}
432 struct AnotherDerived
: AnotherBase
{
433 constexpr AnotherDerived(int i
) : AnotherBase(i
) {}
435 constexpr AnotherBase
Derp(0); // both-error {{must be initialized by a constant expression}} \
436 // both-note {{in call to 'AnotherBase(0)'}}
438 struct YetAnotherBase
{
440 constexpr YetAnotherBase(int i
) : Val(i
) {}
443 struct YetAnotherDerived
: YetAnotherBase
{
444 using YetAnotherBase::YetAnotherBase
; // both-note {{declared here}}
447 constexpr bool doit() const { return Val
== OtherVal
; }
450 constexpr YetAnotherDerived
Oops(0); // both-error {{must be initialized by a constant expression}} \
451 // both-note {{constructor inherited from base class 'YetAnotherBase' cannot be used in a constant expression}}
454 namespace EmptyCtor
{
455 struct piecewise_construct_t
{ explicit piecewise_construct_t() = default; };
456 constexpr piecewise_construct_t piecewise_construct
=
457 piecewise_construct_t();
460 namespace ConditionalInit
{
463 constexpr S
getS(bool b
) {
464 return b
? S
{12} : S
{13};
467 static_assert(getS(true).a
== 12, "");
468 static_assert(getS(false).a
== 13, "");
471 struct A
{ int m
; const int &f
= m
; };
474 static_assert(a
.m
== 10, "");
475 static_assert(a
.f
== 10, "");
480 constexpr int a() const {
487 static_assert(f
.a() == 1337, "");
494 static_assert(b
.a
.m
== 100, "");
495 static_assert(b
.a
.f
== 100, "");
498 static_assert(b2
.a
.m
== 100, "");
499 static_assert(b2
.a
.f
== 100, "");
500 static_assert(b2
.a
.f
== 101, ""); // both-error {{failed}} \
501 // both-note {{evaluates to '100 == 101'}}
504 namespace PointerArith
{
506 struct B
: A
{ int n
; };
509 constexpr A
*a1
= &b
;
510 constexpr B
*b1
= &b
+ 1;
511 constexpr B
*b2
= &b
+ 0;
513 constexpr A
*a2
= &b
+ 1; // both-error {{must be initialized by a constant expression}} \
514 // both-note {{cannot access base class of pointer past the end of object}}
515 constexpr const int *pn
= &(&b
+ 1)->n
; // both-error {{must be initialized by a constant expression}} \
516 // both-note {{cannot access field of pointer past the end of object}}
519 #if __cplusplus >= 202002L
520 namespace VirtualCalls
{
526 constexpr virtual int foo() {
532 constexpr int foo() override
{
537 constexpr int getFooB(bool b
) {
549 static_assert(getFooB(true) == 3, "");
550 static_assert(getFooB(false) == 6, "");
553 namespace MultipleBases
{
556 constexpr virtual int getInt() const { return 10; }
561 class C
: public A
, public B
{
563 constexpr int getInt() const override
{ return 20; }
566 constexpr int callGetInt(const A
& a
) { return a
.getInt(); }
567 static_assert(callGetInt(C()) == 20, "");
568 static_assert(callGetInt(A()) == 10, "");
571 namespace Destructors
{
575 constexpr Base(int &i
) : i(i
) {i
++;}
576 constexpr virtual ~Base() {i
--;}
579 class Derived
: public Base
{
581 constexpr Derived(int &i
) : Base(i
) {}
582 constexpr virtual ~Derived() {i
--;}
585 constexpr int test() {
590 static_assert(test() == 1);
594 constexpr ~S() { // both-error {{never produces a constant expression}}
595 int i
= 1 / 0; // both-warning {{division by zero}} \
596 // both-note 2{{division by zero}}
599 constexpr int testS() {
600 S
{}; // both-note {{in call to 'S{}.~S()'}}
603 static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
604 // both-note {{in call to 'testS()'}}
607 namespace BaseToDerived
{
610 struct B
: A
{ int n
; };
613 constexpr C
*pb
= (C
*)((A
*)&c
+ 1); // both-error {{must be initialized by a constant expression}} \
614 // both-note {{cannot access derived class of pointer past the end of object}}
621 constexpr B() : n(10) {}
624 constexpr C() : B() {}
628 constexpr const A
*pa
= &c
;
629 constexpr const C
*cp
= (C
*)pa
;
630 constexpr const B
*cb
= (B
*)cp
;
632 static_assert(cb
->n
== 10);
633 static_assert(cp
->n
== 10);
637 struct Base
{ int *a
; };
638 struct Base2
: Base
{ int f
[12]; };
640 struct Middle1
{ int b
[3]; };
641 struct Middle2
: Base2
{ char c
; };
642 struct Middle3
: Middle2
{ char g
[3]; };
643 struct Middle4
{ int f
[3]; };
644 struct Middle5
: Middle4
, Middle3
{ char g2
[3]; };
646 struct NotQuiteDerived
: Middle1
, Middle5
{ bool d
; };
647 struct Derived
: NotQuiteDerived
{ int e
; };
649 constexpr NotQuiteDerived NQD1
= {};
651 constexpr Middle5
*M4
= (Middle5
*)((Base2
*)&NQD1
);
652 static_assert(M4
->a
== nullptr);
653 static_assert(M4
->g2
[0] == 0);
658 namespace VirtualDtors
{
662 constexpr A(unsigned &v
) : v(v
) {}
663 constexpr virtual ~A() {
669 constexpr B(unsigned &v
) : A(v
) {}
670 constexpr virtual ~B() {
676 constexpr C(unsigned &v
) : B(v
) {}
677 constexpr virtual ~C() {
682 constexpr bool foo() {
687 return ((a
& (1 << 0)) && (a
& (1 << 1)) && (a
& (1 << 2)));
690 static_assert(foo());
693 namespace QualifiedCalls
{
696 constexpr virtual int foo() const {
700 class B
: public A
{};
703 constexpr int foo() const override
{
704 return B::foo(); // B doesn't have a foo(), so this should call A::foo().
706 constexpr int foo2() const {
707 return this->A::foo();
711 static_assert(c
.foo() == 5);
712 static_assert(c
.foo2() == 5);
717 virtual constexpr int foo() const { return 1; }
725 constexpr int foo() const override
{
731 static_assert(ss
.a
== 1);
739 constexpr Base() : i(func()) {
742 constexpr Base(int i
) : i(i
), j(i
) {}
744 constexpr virtual int func() const { return 1; }
747 struct Derived
: Base
{
748 constexpr Derived() {}
749 constexpr Derived(int i
) : Base(i
) {}
750 constexpr int func() const override
{ return 2; }
753 struct Derived2
: Derived
{
754 constexpr Derived2() : Derived(func()) {} // ref-note {{subexpression not valid in a constant expression}}
755 constexpr int func() const override
{ return 3; }
759 static_assert(B
.i
== 1 && B
.j
== 1, "");
762 static_assert(D
.i
== 1, ""); // expected-error {{static assertion failed}} \
763 // expected-note {{2 == 1}}
764 static_assert(D
.j
== 1, ""); // expected-error {{static assertion failed}} \
765 // expected-note {{2 == 1}}
767 constexpr Derived2 D2
; // ref-error {{must be initialized by a constant expression}} \
768 // ref-note {{in call to 'Derived2()'}} \
769 // ref-note 2{{declared here}}
770 static_assert(D2
.i
== 3, ""); // ref-error {{not an integral constant expression}} \
771 // ref-note {{initializer of 'D2' is not a constant expression}}
772 static_assert(D2
.j
== 3, ""); // ref-error {{not an integral constant expression}} \
773 // ref-note {{initializer of 'D2' is not a constant expression}}
777 namespace VirtualFunctionPointers
{
779 virtual constexpr int func() const { return 1; }
783 constexpr Middle(int i
) : i(i
) {}
788 constexpr Other(int k
) : k(k
) {}
792 struct S2
: Middle
, Other
{
794 constexpr S2(int i
, int j
, int k
) : Middle(i
), Other(k
), j(j
) {}
795 virtual constexpr int func() const { return i
+ j
+ k
+ S::func(); }
799 constexpr decltype(&S::func
) foo
= &S::func
;
800 constexpr int value
= (s
.*foo
)();
801 static_assert(value
== 1);
804 constexpr S2
s2(1, 2, 3);
805 static_assert(s2
.i
== 1);
806 static_assert(s2
.j
== 2);
807 static_assert(s2
.k
== 3);
809 constexpr int value2
= s2
.func();
810 constexpr int value3
= (s2
.*foo
)();
811 static_assert(value3
== 7);
813 constexpr int dynamicDispatch(const S
&s
) {
814 constexpr decltype(&S::func
) SFunc
= &S::func
;
819 static_assert(dynamicDispatch(s
) == 1);
820 static_assert(dynamicDispatch(s2
) == 7);
826 #if __cplusplus < 202002L
827 namespace VirtualFromBase
{
829 virtual int f() const;
834 template <typename T
> struct X
: T
{
837 constexpr int f() { return sizeof(T
); }
840 // Non-virtual f(), OK.
841 constexpr X
<X
<S1
>> xxs1
;
842 constexpr X
<S1
> *p
= const_cast<X
<X
<S1
>>*>(&xxs1
);
843 static_assert(p
->f() == sizeof(S1
), "");
845 // Virtual f(), not OK.
846 constexpr X
<X
<S2
>> xxs2
;
847 constexpr X
<S2
> *q
= const_cast<X
<X
<S2
>>*>(&xxs2
);
848 static_assert(q
->f() == sizeof(X
<S2
>), ""); // both-error {{not an integral constant expression}} \
849 // both-note {{cannot evaluate call to virtual function}}
853 namespace CompositeDefaultArgs
{
857 constexpr Foo() : a(12), b(13) {}
864 constexpr int someFunc(Foo F
= Foo()) {
870 constexpr bool testMe() {
875 static_assert(testMe(), "");
878 constexpr bool BPand(BoolPair bp
) {
879 return bp
.first
&& bp
.second
;
881 static_assert(BPand(BoolPair
{true, false}) == false, "");
883 namespace TemporaryObjectExpr
{
886 constexpr F() : a(12) {}
888 constexpr int foo(F f
) {
891 static_assert(foo(F()) == 0, "");
906 constexpr int foo(A x
) {
907 return x
.a
+ static_cast<int>(x
.b
) + x
.c
[0] + x
.c
[3] + static_cast<int>(x
.d
);
909 static_assert(foo(A()) == 0, "");
912 namespace Inheritance
{
917 constexpr int foo(F2 f
) {
918 return (int)f
.f
+ f
.a
;
920 static_assert(foo(F2()) == 0, "");
923 namespace BitFields
{
927 constexpr int foo(F f
) {
930 static_assert(foo(F()) == 0, "");
944 constexpr int foo(F f
) {
945 return f
.i
+ f
.f2
.f
+ f
.f2
.c
;
947 static_assert(foo(F()) == 0, "");
950 namespace CompositeArrays
{
961 constexpr int foo(F f
) {
962 return f
.i
+ f
.f2
[0].f
+ f
.f2
[0].c
+ f
.f2
[1].f
+ f
.f2
[1].c
;
964 static_assert(foo(F()) == 0, "");
967 #if __cplusplus > 201402L
978 constexpr int foo(F f
) {
979 return f
.i
+ f
.U
.f
; // both-note {{read of member 'f' of union with active member 'a'}}
981 static_assert(foo(F()) == 0, ""); // both-error {{not an integral constant expression}} \
982 // both-note {{in call to}}
986 #if __cplusplus >= 202002L
992 constexpr int foo(S x
) {
995 static_assert(foo(S()) == 0, "");
1000 #if __cplusplus >= 202002L
1001 namespace ParenInit
{
1010 constexpr B
b(A(1),2);
1019 constinit O
o2(0); // both-error {{variable does not have a constant initializer}} \
1020 // both-note {{required by 'constinit' specifier}} \
1021 // both-note {{reference to temporary is not a constant expression}} \
1022 // both-note {{temporary created here}}
1025 /// Initializing an array.
1026 constexpr void bar(int i
, int j
) {
1032 namespace DelegatingConstructors
{
1035 constexpr S() : S(10) {}
1036 constexpr S(int a
) : a(a
) {}
1039 static_assert(s
.a
== 10, "");
1045 constexpr B(int a
) : a(a
), b(a
+ 2) {}
1048 constexpr A() : B(10) {};
1050 constexpr A d4
= {};
1051 static_assert(d4
.a
== 10, "");
1052 static_assert(d4
.b
== 12, "");
1055 namespace AccessOnNullptr
{
1060 constexpr int a() { // both-error {{never produces a constant expression}}
1063 f
->a
= 0; // both-note 2{{cannot access field of null pointer}}
1066 static_assert(a() == 0, ""); // both-error {{not an integral constant expression}} \
1067 // both-note {{in call to 'a()'}}
1069 constexpr int a2() { // both-error {{never produces a constant expression}}
1073 const int *a
= &(f
->a
); // both-note 2{{cannot access field of null pointer}}
1076 static_assert(a2() == 0, ""); // both-error {{not an integral constant expression}} \
1077 // both-note {{in call to 'a2()'}}
1080 namespace IndirectFieldInit
{
1081 #if __cplusplus >= 202002L
1088 constexpr Nested1(int x
) : first(12), x() { x
= 4; }
1089 constexpr Nested1() : Nested1(42) {}
1091 constexpr Nested1 N1
{};
1092 static_assert(N1
.first
== 12, "");
1096 struct First
{ int x
= 42; };
1101 constexpr Nested2(int x
) : first(12), x() { x
= 4; }
1102 constexpr Nested2() : Nested2(42) {}
1104 constexpr Nested2 N2
{};
1105 static_assert(N2
.first
.x
== 12, "");
1113 constexpr Nested3(int x
) : first(3), x() { x
= 4; }
1114 constexpr Nested3() : Nested3(42) {}
1117 constexpr Nested3 N3
{};
1118 static_assert(N3
.first
== 3, "");
1120 /// Test that we get the offset right if the
1121 /// record has a base.
1122 struct Nested4Base
{
1127 struct Nested4
: Nested4Base
{
1132 constexpr Nested4(int x
) : first(123), x() { a
= 1; b
= 2; c
= 3; x
= 4; }
1133 constexpr Nested4() : Nested4(42) {}
1135 constexpr Nested4 N4
{};
1136 static_assert(N4
.first
== 123, "");
1143 constexpr S(int x_
, int y_
) : x(x_
), y(y_
) {}
1146 constexpr S
s(1, 2);
1147 static_assert(s
.x
== 1 && s
.y
== 2);
1158 constexpr S2(int x_
, int y_
) : a(3), b(4), x(x_
), y(y_
) {}
1161 constexpr S2
s2(1, 2);
1162 static_assert(s2
.x
== 1 && s2
.y
== 2 && s2
.a
== 3 && s2
.b
== 4);
1167 namespace InheritedConstructor
{
1172 constexpr A(int c
, int d
) : c(c
), d(d
){}
1174 struct B
: A
{ using A::A
; };
1176 constexpr B b
= {13, 1};
1177 static_assert(b
.c
== 13, "");
1178 static_assert(b
.d
== 1, "");
1181 namespace PR47555_2
{
1186 constexpr A(int c
, int &d
, double e
) : c(c
), d(++d
), e(e
){}
1188 struct B
: A
{ using A::A
; };
1192 B b
= {10, a
, 40.0};
1195 static_assert(f() == 11, "");
1198 namespace AaronsTest
{
1200 constexpr T(float) {}
1204 constexpr Base(T t
= 1.0f
) {}
1205 constexpr Base(float) {}
1208 struct FirstMiddle
: Base
{
1210 constexpr FirstMiddle() : Base(2.0f
) {}
1213 struct SecondMiddle
: Base
{
1214 constexpr SecondMiddle() : Base(3.0f
) {}
1215 constexpr SecondMiddle(T t
) : Base(t
) {}
1218 struct S
: FirstMiddle
, SecondMiddle
{
1219 using FirstMiddle::FirstMiddle
;
1220 constexpr S(int i
) : S(4.0f
) {}
1227 namespace InvalidCtorInitializer
{
1231 : Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
1233 // no crash on evaluating the constexpr ctor.
1234 constexpr int Z
= X().Y
; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}}
1237 extern int f(); // both-note {{here}}
1238 struct HasNonConstExprMemInit
{
1239 int x
= f(); // both-note {{non-constexpr function}}
1240 constexpr HasNonConstExprMemInit() {} // both-error {{never produces a constant expression}}
1244 template <class Tp
, Tp v
>
1245 struct integral_constant
{
1246 static const Tp value
= v
;
1249 template <class Tp
, Tp v
>
1250 const Tp integral_constant
<Tp
, v
>::value
;
1252 typedef integral_constant
<bool, true> true_type
;
1253 typedef integral_constant
<bool, false> false_type
;
1255 /// This might look innocent, but we get an evaluateAsInitializer call for the
1256 /// static bool member before evaluating the first static_assert, but we do NOT
1257 /// get such a call for the second one. So the second one needs to lazily visit
1258 /// the data member itself.
1259 static_assert(true_type::value
, "");
1260 static_assert(true_type::value
, "");
1263 #if __cplusplus >= 202002L
1265 /// Used to crash because the CXXDefaultInitExpr is of compound type.
1268 constexpr ~A() { --x
; }
1277 B
{x
}; // both-warning {{expression result unused}}
1286 static const int sz
;
1287 static const int sz2
;
1289 const int A1::sz2
= 11;
1290 template<typename T
>
1293 // both-warning@-1 {{variable length arrays in C++ are a Clang extension}}
1294 // both-note@-2 {{initializer of 'sz' is unknown}}
1295 // both-note@-9 {{declared here}}
1297 template<typename T
>
1301 const int A1::sz
= 12;
1310 static constexpr int Z
= 12;
1313 static_assert(f
.Z
== 12, "");
1316 namespace UnnamedBitFields
{
1324 constexpr A a
= (A
){1.0, 'a'};
1325 static_assert(a
.f
== 1.0, "");
1326 static_assert(a
.c
== 'a', "");
1329 namespace VirtualBases
{
1330 /// This used to crash.
1336 class B
: public virtual A
{
1338 int getX() { return x
; } // both-note {{declared here}}
1341 class DV
: virtual public B
{};
1345 int a
[b
.getX()]; // both-warning {{variable length arrays}} \
1346 // both-note {{non-constexpr function 'getX' cannot be used}}
1351 struct U
{ int n
; };
1352 struct A
: virtual U
{ int n
; };
1355 static_assert((U
*)(A
*)(&a
) == (U
*)(&a
), "");
1357 struct C
: virtual A
{};
1360 constexpr B
*p
= &d
;
1361 constexpr C
*q
= &d
;
1362 static_assert((A
*)p
== (A
*)q
, ""); // both-error {{failed}}
1366 struct U
{ int n
; };
1367 struct V
: U
{ int n
; };
1368 struct A
: virtual V
{ int n
; };
1369 struct Aa
{ int n
; };
1370 struct B
: virtual A
, Aa
{};
1372 struct C
: virtual A
, Aa
{};
1378 constexpr B
*p
= &d
;
1379 constexpr C
*q
= &d
;
1381 static_assert((void*)p
!= (void*)q
, "");
1382 static_assert((A
*)p
== (A
*)q
, "");
1383 static_assert((Aa
*)p
!= (Aa
*)q
, "");
1387 constexpr V
*x
= (A
*)p
;
1388 static_assert(v
== w
, "");
1389 static_assert(v
== x
, "");
1391 static_assert((U
*)&d
== p
, "");
1392 static_assert((U
*)&d
== q
, "");
1393 static_assert((U
*)&d
== v
, "");
1394 static_assert((U
*)&d
== w
, "");
1395 static_assert((U
*)&d
== x
, "");
1398 struct Y1
: virtual X
{};
1400 struct Z
: Y1
, Y2
{};
1402 static_assert((X
*)(Y1
*)&z
!= (X
*)(Y2
*)&z
, "");
1406 namespace ZeroInit
{
1409 S3(const S3
&) = default;
1411 constexpr S3(int n
) : n(n
) {}
1414 constexpr S3 s3d
; // both-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
1415 static_assert(s3d
.n
== 0, "");
1420 static_assert(P().a
== 10, "");
1424 #if __cplusplus >= 202002L
1426 template <unsigned N
> constexpr C(const char (&)[N
]) : n(N
) {}
1430 constexpr auto operator""_c() { return c
.n
; }
1432 constexpr auto waldo
= "abc"_c
;
1433 static_assert(waldo
== 4, "");
1438 namespace TemporaryWithInvalidDestructor
{
1439 #if __cplusplus >= 202002L
1442 constexpr ~A() noexcept(false) { // both-error {{never produces a constant expression}}
1443 throw; // both-note 2{{not valid in a constant expression}} \
1444 // both-error {{cannot use 'throw' with exceptions disabled}}
1447 static_assert(A().a
, ""); // both-error {{not an integral constant expression}} \
1448 // both-note {{in call to}}
1452 namespace IgnoredCtorWithZeroInit
{
1462 #if __cplusplus >= 202002L
1463 namespace VirtOperator
{
1464 /// This used to crash because it's a virtual CXXOperatorCallExpr.
1466 virtual constexpr bool operator==(const B
&) const { return true; }
1469 constexpr bool operator==(const B
&) const override
{ return false; } // both-note {{operator}}
1471 constexpr bool cmp_base_derived
= D() == D(); // both-warning {{ambiguous}}
1474 namespace FloatAPValue
{
1475 struct ClassTemplateArg
{
1479 template<ClassTemplateArg A
> struct ClassTemplateArgTemplate
{
1480 static constexpr const ClassTemplateArg
&Arg
= A
;
1482 ClassTemplateArgTemplate
<ClassTemplateArg
{1, 2.0f
}> ClassTemplateArgObj
;
1483 template<const ClassTemplateArg
&> struct ClassTemplateArgRefTemplate
{};
1484 ClassTemplateArgRefTemplate
<ClassTemplateArgObj
.Arg
> ClassTemplateArgRefObj
;
1488 namespace LocalWithThisPtrInit
{
1493 constexpr int foo() {
1497 static_assert(foo() == 2, "");
1500 namespace OnePastEndAndBack
{
1507 constexpr const Base
*c
= &a
+ 1;
1508 constexpr const Base
*d
= c
- 1;
1509 static_assert(d
== &a
, "");
1517 constexpr Bitset() {
1518 int Init
[2] = {1,2};
1522 constexpr void set(unsigned I
) {
1524 this->Bit
= 1u << 1;
1532 constexpr ArchInfo ARMV8A
= {
1537 namespace ArrayInitChain
{
1538 struct StringLiteral
{
1542 struct CustomOperandVal
{
1545 unsigned Mask
= Width
+ 1;
1548 constexpr CustomOperandVal A
[] = {
1550 {{"depctr_hold_cnt"}, 12, 13},
1552 static_assert(A
[0].Str
.S
== nullptr, "");
1553 static_assert(A
[0].Width
== 0, "");
1554 static_assert(A
[0].Mask
== 1, "");
1556 static_assert(A
[1].Width
== 12, "");
1557 static_assert(A
[1].Mask
== 13, "");
1560 #if __cplusplus >= 202002L
1561 namespace ctorOverrider
{
1562 // Ensure that we pick the right final overrider during construction.
1564 virtual constexpr char f() const { return 'A'; }
1572 constexpr Covariant1 cb
;
1576 #if __cplusplus >= 202002L
1577 namespace VirtDtor
{
1578 struct X
{ char *p
; constexpr ~X() { *p
++ = 'X'; } };
1579 struct Y
: X
{ int y
; virtual constexpr ~Y() { *p
++ = 'Y'; } };
1580 struct Z
: Y
{ int z
; constexpr ~Z() override
{ *p
++ = 'Z'; } };
1583 constexpr VU() : z() {}
1588 constexpr char virt_dtor(int mode
, const char *expected
) {
1596 static_assert(virt_dtor(0, "ZYX"));
1599 namespace DtorDestroysFieldsAfterSelf
{
1611 constexpr F(int a
, int &b
) : a(a
), b(b
) {}
1617 constexpr int foo() {
1627 static_assert(foo() == 15);
1631 namespace ExprWithCleanups
{
1632 struct A
{ A(); ~A(); int get(); };
1633 constexpr int get() {return false ? A().get() : 1;}
1634 static_assert(get() == 1, "");
1639 constexpr S(int V
) : V(V
) {}
1640 constexpr int get() {
1644 constexpr int get(bool b
) {
1645 S a
= b
? S(1) : S(2);
1649 static_assert(get(true) == 1, "");
1650 static_assert(get(false) == 2, "");
1653 constexpr auto F
= true ? 1i
: 2i
;
1654 static_assert(F
== 1i
, "");
1657 namespace NullptrUpcast
{
1659 struct B
: A
{ int n
; };
1660 constexpr B
*nb
= nullptr;
1661 constexpr A
&ra
= *nb
; // both-error {{constant expression}} \
1662 // both-note {{cannot access base class of null pointer}}
1665 namespace NonConst
{
1668 static constexpr int Size
= I
;
1669 constexpr int getSize() const { return I
; }
1670 explicit S(int a
) {}
1676 static_assert(s
.getSize() == 10, "");