1 // RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++11 -Werror=c++1y-extensions %s
2 // RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++1y -DCXX1Y %s
12 struct NonLiteral { // expected-note 3{{no constexpr constructors}}
17 constexpr Literal() {}
18 operator int() const { return 0; }
22 virtual int ImplicitlyVirtual() const = 0; // expected-note {{overridden virtual function}}
25 int ImplicitlyVirtual() const;
28 // The definition of a constexpr function shall satisfy the following
30 struct T : SS, NonLiteral {
32 constexpr int f() const;
34 // - it shall not be virtual;
35 virtual constexpr int ExplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
37 constexpr int ImplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
39 virtual constexpr int OutOfLineVirtual() const; // expected-error {{virtual function cannot be constexpr}}
41 // - its return type shall be a literal type;
42 constexpr NonLiteral NonLiteralReturn() const { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
43 constexpr void VoidReturn() const { return; }
45 // expected-error@-2 {{constexpr function's return type 'void' is not a literal type}}
47 constexpr ~T(); // expected-error {{destructor cannot be marked constexpr}}
48 typedef NonLiteral F() const;
49 constexpr F NonLiteralReturn2; // ok until definition
51 // - each of its parameter types shall be a literal type;
52 constexpr int NonLiteralParam(NonLiteral) const { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
53 typedef int G(NonLiteral) const;
54 constexpr G NonLiteralParam2; // ok until definition
56 // - its function-body shall be = delete, = default,
57 constexpr int Deleted() const = delete;
58 // It's not possible for the function-body to legally be "= default" here
59 // (that is, for a non-constructor function) in C++11.
60 // Other than constructors, only the copy- and move-assignment operators and
61 // destructor can be defaulted. Destructors can't be constexpr since they
62 // don't have a literal return type. Defaulted assignment operators can't be
63 // constexpr since they can't be const.
64 constexpr T &operator=(const T&) = default;
66 // expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
67 // expected-warning@-3 {{C++14}}
69 // expected-error@-5 {{defaulted definition of copy assignment operator is not constexpr}}
73 constexpr int T::OutOfLineVirtual() const { return 0; }
77 constexpr T2 &operator=(const T2&) = default; // ok
80 constexpr T3 &operator=(const T3&) const = default;
81 // expected-error@-1 {{an explicitly-defaulted copy assignment operator may not have 'const' or 'volatile' qualifiers}}
85 constexpr U SelfReturn() const;
86 constexpr int SelfParam(U) const;
89 struct V : virtual U { // expected-note {{here}}
90 constexpr int F() const { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
93 // or a compound-statememt that contains only [CXX11]
94 constexpr int AllowedStmtsCXX11() {
98 // - static_assert-declarations
99 static_assert(true, "the impossible happened!");
101 // - typedef declarations and alias-declarations that do not define classes
106 using K = int[sizeof(I) + sizeof(J)];
107 // Note, the standard requires we reject this.
110 // - using-declarations
113 // - using-directives
116 // - and exactly one return statement
117 return sizeof(K) + sizeof(C) + sizeof(K);
120 // or a compound-statement that does not contain [CXX1Y]
121 constexpr int DisallowedStmtsCXX1Y_1() {
122 // - an asm-definition
123 asm("int3"); // expected-error {{statement not allowed in constexpr function}}
126 constexpr int DisallowedStmtsCXX1Y_2() {
127 // - a goto statement
128 goto x; // expected-error {{statement not allowed in constexpr function}}
132 constexpr int DisallowedStmtsCXX1Y_3() {
134 try {} catch (...) {} // expected-error {{statement not allowed in constexpr function}}
137 constexpr int DisallowedStmtsCXX1Y_4() {
138 // - a definition of a variable of non-literal type
139 NonLiteral nl; // expected-error {{variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function}}
142 constexpr int DisallowedStmtsCXX1Y_5() {
143 // - a definition of a variable of static storage duration
144 static constexpr int n = 123; // expected-error {{static variable not permitted in a constexpr function}}
147 constexpr int DisallowedStmtsCXX1Y_6() {
148 // - a definition of a variable of thread storage duration
149 thread_local constexpr int n = 123; // expected-error {{thread_local variable not permitted in a constexpr function}}
152 constexpr int DisallowedStmtsCXX1Y_7() {
153 // - a definition of a variable for which no initialization is performed
154 int n; // expected-error {{variables defined in a constexpr function must be initialized}}
158 constexpr int ForStmt() {
159 for (int n = 0; n < 10; ++n)
161 // expected-error@-2 {{statement not allowed in constexpr function}}
165 constexpr int VarDecl() {
168 // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
172 constexpr int ConstexprVarDecl() {
175 // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
179 constexpr int VarWithCtorDecl() {
182 // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
187 constexpr NonLiteral &ExternNonLiteralVarDecl() {
188 extern NonLiteral nl;
190 // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
194 static_assert(&ExternNonLiteralVarDecl() == &nl, "");
195 constexpr int FuncDecl() {
196 constexpr int ForwardDecl(int);
198 // expected-error@-2 {{use of this statement in a constexpr function is a C++14 extension}}
200 return ForwardDecl(42);
202 constexpr int ClassDecl1() {
203 typedef struct { } S1;
205 // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
209 constexpr int ClassDecl2() {
210 using S2 = struct { };
212 // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
216 constexpr int ClassDecl3() {
219 // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
223 constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}}
224 constexpr int MultiReturn() {
228 // expected-error@-2 {{multiple return statements in constexpr function}}
229 // expected-note@-4 {{return statement}}
233 // - every constructor call and implicit conversion used in initializing the
234 // return value shall be one of those allowed in a constant expression.
236 // We implement the proposed resolution of DR1364 and ignore this bullet.
237 // However, we implement the spirit of the check as part of the p5 checking that
238 // a constexpr function must be able to produce a constant expression.
240 constexpr int f(int k) {
241 return k; // ok, even though lvalue-to-rvalue conversion of a function
242 // parameter is not allowed in a constant expression.
244 int kGlobal; // expected-note {{here}}
245 constexpr int f() { // expected-error {{constexpr function never produces a constant expression}}
246 return kGlobal; // expected-note {{read of non-const}}
250 namespace rdar13584715 {
251 typedef __PTRDIFF_TYPE__ ptrdiff_t;
253 template<typename T> struct X {
257 void foo(ptrdiff_t id) {
259 case reinterpret_cast<ptrdiff_t>(&X<long>::value): // expected-error{{case value is not a constant expression}} \
260 // expected-note{{reinterpret_cast is not allowed in a constant expression}}
266 namespace std_example {
267 constexpr int square(int x) {
270 constexpr long long_max() {
273 constexpr int abs(int x) {
276 // expected-error@-2 {{C++14}}
281 constexpr int first(int n) {
282 static int value = n; // expected-error {{static variable not permitted}}
285 constexpr int uninit() {
286 int a; // expected-error {{must be initialized}}
289 constexpr int prev(int x) {
293 // expected-error@-4 {{never produces a constant expression}}
294 // expected-note@-4 {{subexpression}}
296 constexpr int g(int x, int n) {
298 while (--n > 0) r *= x;
302 // expected-error@-5 {{C++14}}
303 // expected-error@-5 {{statement not allowed}}