29
30
struct T : SS, NonLiteral { // expected-note {{base class 'NonLiteral' of non-literal type}}
31
constexpr int f(); // expected-error {{non-literal type 'T' cannot have constexpr members}}
32
constexpr int f() const; // expected-error {{non-literal type 'T' cannot have constexpr members}}
33
34
// - it shall not be virtual;
34
virtual constexpr int ExplicitlyVirtual() { return 0; } // expected-error {{virtual function cannot be constexpr}}
35
virtual constexpr int ExplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
36
constexpr int ImplicitlyVirtual() { return 0; } // expected-error {{virtual function cannot be constexpr}}
37
constexpr int ImplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
38
39
// - its return type shall be a literal type;
39
constexpr NonLiteral NonLiteralReturn() { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
40
constexpr void VoidReturn() { return; } // expected-error {{constexpr function's return type 'void' is not a literal type}}
40
constexpr NonLiteral NonLiteralReturn() const { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
41
constexpr void VoidReturn() const { return; }
43
// expected-error@-2 {{constexpr function's return type 'void' is not a literal type}}
41
45
constexpr ~T(); // expected-error {{destructor cannot be marked constexpr}}
42
typedef NonLiteral F();
46
typedef NonLiteral F() const;
43
47
constexpr F NonLiteralReturn2; // ok until definition
45
49
// - each of its parameter types shall be a literal type;
46
constexpr int NonLiteralParam(NonLiteral) { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
47
typedef int G(NonLiteral);
50
constexpr int NonLiteralParam(NonLiteral) const { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
51
typedef int G(NonLiteral) const;
48
52
constexpr G NonLiteralParam2; // ok until definition
50
54
// - its function-body shall be = delete, = default,
51
constexpr int Deleted() = delete;
52
// It's not possible for the function-body to legally be "= default" here.
55
constexpr int Deleted() const = delete;
56
// It's not possible for the function-body to legally be "= default" here
57
// (that is, for a non-constructor function) in C++11.
53
58
// Other than constructors, only the copy- and move-assignment operators and
54
59
// destructor can be defaulted. Destructors can't be constexpr since they
55
60
// don't have a literal return type. Defaulted assignment operators can't be
56
61
// constexpr since they can't be const.
57
constexpr T &operator=(const T&) = default; // expected-error {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
62
constexpr T &operator=(const T&) = default;
64
// expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
65
// expected-warning@-3 {{C++1y}}
67
// expected-error@-5 {{defaulted definition of copy assignment operator is not constexpr}}
73
constexpr T2 &operator=(const T2&) = default; // ok
76
constexpr T3 &operator=(const T3&) const = default;
77
// expected-error@-1 {{an explicitly-defaulted copy assignment operator may not have 'const' or 'volatile' qualifiers}}
60
constexpr U SelfReturn();
61
constexpr int SelfParam(U);
81
constexpr U SelfReturn() const;
82
constexpr int SelfParam(U) const;
64
85
struct V : virtual U { // expected-note {{here}}
65
constexpr int F() { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
86
constexpr int F() const { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
68
// or a compound-statememt that contains only
69
constexpr int AllowedStmts() {
89
// or a compound-statememt that contains only [CXX11]
90
constexpr int AllowedStmtsCXX11() {
70
91
// - null statements
91
112
// - and exactly one return statement
92
113
return sizeof(K) + sizeof(C) + sizeof(K);
116
// or a compound-statement that does not contain [CXX1Y]
117
constexpr int DisallowedStmtsCXX1Y_1() {
118
// - an asm-definition
119
asm("int3"); // expected-error {{statement not allowed in constexpr function}}
122
constexpr int DisallowedStmtsCXX1Y_2() {
123
// - a goto statement
124
goto x; // expected-error {{statement not allowed in constexpr function}}
128
constexpr int DisallowedStmtsCXX1Y_3() {
130
try {} catch (...) {} // expected-error {{statement not allowed in constexpr function}}
133
constexpr int DisallowedStmtsCXX1Y_4() {
134
// - a definition of a variable of non-literal type
135
NonLiteral nl; // expected-error {{variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function}}
138
constexpr int DisallowedStmtsCXX1Y_5() {
139
// - a definition of a variable of static storage duration
140
static constexpr int n = 123; // expected-error {{static variable not permitted in a constexpr function}}
143
constexpr int DisallowedStmtsCXX1Y_6() {
144
// - a definition of a variable of thread storage duration
145
thread_local constexpr int n = 123; // expected-error {{thread_local variable not permitted in a constexpr function}}
148
constexpr int DisallowedStmtsCXX1Y_7() {
149
// - a definition of a variable for which no initialization is performed
150
int n; // expected-error {{variables defined in a constexpr function must be initialized}}
94
154
constexpr int ForStmt() {
95
for (int n = 0; n < 10; ++n) // expected-error {{statement not allowed in constexpr function}}
155
for (int n = 0; n < 10; ++n)
157
// expected-error@-2 {{statement not allowed in constexpr function}}
98
161
constexpr int VarDecl() {
99
constexpr int a = 0; // expected-error {{variables cannot be declared in a constexpr function}}
164
// expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
168
constexpr int ConstexprVarDecl() {
171
// expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
175
constexpr int VarWithCtorDecl() {
178
// expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
183
constexpr NonLiteral &ExternNonLiteralVarDecl() {
184
extern NonLiteral nl;
186
// expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
190
static_assert(&ExternNonLiteralVarDecl() == &nl, "");
102
191
constexpr int FuncDecl() {
103
constexpr int ForwardDecl(int); // expected-error {{statement not allowed in constexpr function}}
192
constexpr int ForwardDecl(int);
194
// expected-error@-2 {{use of this statement in a constexpr function is a C++1y extension}}
104
196
return ForwardDecl(42);
106
198
constexpr int ClassDecl1() {
107
typedef struct { } S1; // expected-error {{types cannot be defined in a constexpr function}}
199
typedef struct { } S1;
201
// expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
110
205
constexpr int ClassDecl2() {
111
using S2 = struct { }; // expected-error {{types cannot be defined in a constexpr function}}
206
using S2 = struct { };
208
// expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
114
212
constexpr int ClassDecl3() {
115
struct S3 { }; // expected-error {{types cannot be defined in a constexpr function}}
215
// expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
118
219
constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}}
119
220
constexpr int MultiReturn() {
120
return 0; // expected-note {{return statement}}
121
return 0; // expected-error {{multiple return statements in constexpr function}}
224
// expected-error@-2 {{multiple return statements in constexpr function}}
225
// expected-note@-4 {{return statement}}
124
229
// - every constructor call and implicit conversion used in initializing the
262
namespace std_example {
263
constexpr int square(int x) {
266
constexpr long long_max() {
269
constexpr int abs(int x) {
272
// expected-error@-2 {{C++1y}}
277
constexpr int first(int n) {
278
static int value = n; // expected-error {{static variable not permitted}}
281
constexpr int uninit() {
282
int a; // expected-error {{must be initialized}}
285
constexpr int prev(int x) {
289
// expected-error@-4 {{never produces a constant expression}}
290
// expected-note@-4 {{subexpression}}
292
constexpr int g(int x, int n) {
294
while (--n > 0) r *= x;
298
// expected-error@-5 {{C++1y}}
299
// expected-error@-5 {{statement not allowed}}