| 1 |
// P2242R3 |
| 2 |
// { dg-do compile { target c++14 } } |
| 3 |
|
| 4 |
constexpr int |
| 5 |
foo () |
| 6 |
{ |
| 7 |
lab: // { dg-error "label definition in 'constexpr' function only available with" "" { target c++20_down } } |
| 8 |
return 1; |
| 9 |
} |
| 10 |
|
| 11 |
constexpr int |
| 12 |
bar (int x) |
| 13 |
{ |
| 14 |
if (x) |
| 15 |
goto lab; // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } } |
| 16 |
return 1; |
| 17 |
lab: |
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
constexpr int |
| 22 |
baz (int x) |
| 23 |
{ |
| 24 |
if (!x) |
| 25 |
return 1; |
| 26 |
static int a; // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } } |
| 27 |
return ++a; // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } .-1 } |
| 28 |
} |
| 29 |
|
| 30 |
constexpr int |
| 31 |
qux (int x) |
| 32 |
{ |
| 33 |
if (!x) |
| 34 |
return 1; |
| 35 |
thread_local int a; // { dg-error "'a' defined 'thread_local' in 'constexpr' function only available with" "" { target c++20_down } } |
| 36 |
return ++a; // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } .-1 } |
| 37 |
} |
| 38 |
|
| 39 |
constexpr int |
| 40 |
garply (int x) |
| 41 |
{ |
| 42 |
if (!x) |
| 43 |
return 1; |
| 44 |
extern thread_local int a; // { dg-bogus "'thread_local' in 'constexpr' function only available with" "" { target c++20_down } } |
| 45 |
return ++a; |
| 46 |
} |
| 47 |
|
| 48 |
struct S { S (); ~S (); int s; }; // { dg-message "'S' is not literal because:" "" { target c++20_down } } |
| 49 |
// { dg-message "'S' has a non-trivial destructor" "" { target c++17_down } .-1 } |
| 50 |
// { dg-message "'S' does not have 'constexpr' destructor" "" { target { c++20_only } } .-2 } |
| 51 |
|
| 52 |
constexpr int |
| 53 |
corge (int x) |
| 54 |
{ |
| 55 |
if (!x) |
| 56 |
return 1; |
| 57 |
S s; // { dg-error "variable 's' of non-literal type 'S' in 'constexpr' function only available with" "" { target c++20_down } } |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
#if __cpp_constexpr >= 202110L |
| 62 |
static_assert (foo ()); |
| 63 |
static_assert (bar (0)); |
| 64 |
static_assert (baz (0)); |
| 65 |
static_assert (qux (0)); |
| 66 |
static_assert (garply (0)); |
| 67 |
static_assert (corge (0)); |
| 68 |
#endif |