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