75 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s3 4// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter5// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s -fexperimental-new-constant-interpreter6 7// A constexpr specifier used in an object declaration declares the object as8// const.9constexpr int a = 0;10extern const int a;11 12int i; // expected-note 2{{here}}13constexpr int *b = &i;14extern int *const b;15 16constexpr int &c = i;17extern int &c;18 19constexpr int (*d)(int) = 0;20extern int (*const d)(int);21 22// A variable declaration which uses the constexpr specifier shall have an23// initializer and shall be initialized by a constant expression.24constexpr int ni1; // expected-error {{constexpr variable 'ni1' must be initialized by a constant expression}}25constexpr struct C { C(); } ni2; // expected-error {{cannot have non-literal type 'const struct C'}} expected-note 3{{has no constexpr constructors}}26constexpr double &ni3; // expected-error {{declaration of reference variable 'ni3' requires an initializer}}27 28constexpr int nc1 = i; // expected-error {{constexpr variable 'nc1' must be initialized by a constant expression}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}29constexpr C nc2 = C(); // expected-error {{cannot have non-literal type 'const C'}}30int &f(); // expected-note 2{{declared here}}31constexpr int &nc3 = f(); // expected-error {{constexpr variable 'nc3' must be initialized by a constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}32constexpr int nc4(i); // expected-error {{constexpr variable 'nc4' must be initialized by a constant expression}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}33constexpr C nc5((C())); // expected-error {{cannot have non-literal type 'const C'}}34constexpr int &nc6(f()); // expected-error {{constexpr variable 'nc6' must be initialized by a constant expression}} expected-note {{non-constexpr function 'f'}}35 36struct pixel {37 int x, y;38};39constexpr pixel ur = { 1294, 1024 }; // ok40constexpr pixel origin; // expected-error {{default initialization of an object of const type 'const pixel' without a user-provided default constructor}}41 42#if __cplusplus > 201702L43// A constexpr variable shall have constant destruction.44struct A {45 bool ok;46 constexpr A(bool ok) : ok(ok) {}47 constexpr ~A() noexcept(false) {48 void oops(); // expected-note 6{{declared here}}49 if (!ok) oops(); // expected-note 6{{non-constexpr function}}50 }51};52 53struct B {54 A a[2];55 constexpr B(bool ok) : a{A(!ok), A(ok)}{}56};57 58struct Cons {59 bool val[2];60 constexpr Cons() : val{true, false} {}61};62 63constexpr A const_dtor(true);64static_assert(B(false).a[1].ok); // expected-error {{static assertion expression is not an integral constant expression}} \65 expected-note {{in call to 'B(false).a[1].~A()'}} expected-note {{in call to 'B(false).~B()'}}66static_assert(B(true).a[1].ok); // expected-error {{static assertion expression is not an integral constant expression}} \67 expected-note {{in call to 'B(true).a[0].~A()'}} expected-note {{in call to 'B(true).~B()'}}68static_assert(B(Cons().val[1]).a[1].ok); // expected-error {{static assertion expression is not an integral constant expression}} \69 expected-note {{in call to 'B(Cons().val[1]).a[1].~A()'}} expected-note {{in call to 'B(Cons().val[1]).~B()'}}70static_assert(B((new Cons)->val[0]).a[1].ok); // expected-error {{static assertion expression is not an integral constant expression}} \71 expected-note {{in call to 'B((new Cons)->val[0]).a[0].~A()'}} expected-note {{in call to 'B((new Cons)->val[0]).~B()'}}72constexpr A non_const_dtor(false); // expected-error {{must have constant destruction}} expected-note {{in call}}73constexpr A arr_dtor[5] = {true, true, true, false, true}; // expected-error {{must have constant destruction}} expected-note {{in call to 'arr_dtor[3].~A()'}}74#endif75