| 1 |
// PR c++/106649 |
| 2 |
// P2448 - Relaxing some constexpr restrictions |
| 3 |
// { dg-do compile { target c++14 } } |
| 4 |
// { dg-options "" } |
| 5 |
|
| 6 |
// No constexpr constructors = not a literal type. |
| 7 |
struct NonLiteral { |
| 8 |
NonLiteral() {} |
| 9 |
}; |
| 10 |
|
| 11 |
// C++23: It is possible to write a constexpr function for which no |
| 12 |
// invocation satisfies the requirements of a core constant expression. |
| 13 |
constexpr NonLiteral |
| 14 |
fn0 (int) // { dg-warning "invalid return type" "" { target c++20_down } } |
| 15 |
{ |
| 16 |
return NonLiteral{}; |
| 17 |
} |
| 18 |
|
| 19 |
constexpr int |
| 20 |
fn1 (NonLiteral) // { dg-warning "invalid type" "" { target c++20_down } } |
| 21 |
{ |
| 22 |
return 42; |
| 23 |
} |
| 24 |
|
| 25 |
// From P2448. |
| 26 |
void f(int& i) { |
| 27 |
i = 0; |
| 28 |
} |
| 29 |
|
| 30 |
constexpr void g(int& i) { |
| 31 |
f(i); // { dg-warning "call to" "" { target c++20_down } } |
| 32 |
} |
| 33 |
|
| 34 |
// [dcl.constexpr] used to have this. |
| 35 |
constexpr int f(bool b) |
| 36 |
{ return b ? throw 0 : 0; } // OK |
| 37 |
constexpr int f() { return f(true); } // ill-formed, no diagnostic required |
| 38 |
|
| 39 |
struct B { |
| 40 |
constexpr B(int) : i(0) { } |
| 41 |
int i; |
| 42 |
}; |
| 43 |
|
| 44 |
int global; |
| 45 |
|
| 46 |
struct D : B { |
| 47 |
constexpr D() : B(global) { } // { dg-warning "not usable" "" { target c++20_down } } |
| 48 |
// ill-formed, no diagnostic required |
| 49 |
// lvalue-to-rvalue conversion on non-constant global |
| 50 |
}; |
| 51 |
|
| 52 |
// If no specialization of the template would satisfy the requirements |
| 53 |
// for a constexpr function when considered as a non-template function, |
| 54 |
// the template is ill-formed, no diagnostic required. |
| 55 |
template<typename> |
| 56 |
constexpr void |
| 57 |
fn2 () |
| 58 |
{ |
| 59 |
int i = 42; |
| 60 |
f (i); |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
fn3 () |
| 65 |
{ |
| 66 |
fn2<int>(); |
| 67 |
} |
| 68 |
|
| 69 |
constexpr volatile int cvi = 10; |
| 70 |
|
| 71 |
constexpr int |
| 72 |
fn4 () |
| 73 |
{ |
| 74 |
return cvi; // { dg-warning "lvalue-to-rvalue conversion" "" { target c++20_down } } |
| 75 |
} |
| 76 |
|
| 77 |
constexpr unsigned int |
| 78 |
fn5 (int *p) |
| 79 |
{ |
| 80 |
unsigned int *q = reinterpret_cast<unsigned int *>(p); // { dg-warning "reinterpret_cast" "" { target c++20_down } } |
| 81 |
return *q; |
| 82 |
} |
| 83 |
|
| 84 |
constexpr int |
| 85 |
fn6 (int i) |
| 86 |
{ |
| 87 |
void *p = (void *) 1LL; // { dg-warning ".reinterpret_cast. from integer to pointer" "" { target c++20_down } } |
| 88 |
return 42; |
| 89 |
} |
| 90 |
|
| 91 |
constexpr int |
| 92 |
fn7 (int i) |
| 93 |
{ |
| 94 |
static int s = i; // { dg-error "static" "" { target c++20_down } } |
| 95 |
return s; |
| 96 |
} |