p3.cpp revision 8398cbfc73f7ff0a676232db9ea9160bb9dafd8d
1// RUN: %clang_cc1 -verify -std=c++11 %s 2 3namespace N { 4 typedef char C; 5} 6 7namespace M { 8 typedef double D; 9} 10 11struct NonLiteral { // expected-note 4{{no constexpr constructors}} 12 NonLiteral() {} 13 NonLiteral(int) {} 14}; 15struct Literal { 16 constexpr Literal() {} 17 operator int() const { return 0; } 18}; 19 20struct S { 21 virtual int ImplicitlyVirtual() const = 0; // expected-note {{overridden virtual function}} 22}; 23struct SS : S { 24 int ImplicitlyVirtual() const; 25}; 26 27// Note, the wording applies constraints to the definition of constexpr 28// functions, but we intentionally apply all that we can to the declaration 29// instead. See DR1360. 30 31// The definition of a constexpr function shall satisfy the following 32// constraints: 33struct T : SS, NonLiteral { // expected-note {{base class 'NonLiteral' of non-literal type}} 34 constexpr T(); // expected-error {{non-literal type 'T' cannot have constexpr members}} 35 36 // - it shall not be virtual; 37 virtual constexpr int ExplicitlyVirtual(); // expected-error {{virtual function cannot be constexpr}} 38 39 constexpr int ImplicitlyVirtual(); // expected-error {{virtual function cannot be constexpr}} 40 41 // - its return type shall be a literal type; 42 constexpr NonLiteral NonLiteralReturn(); // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}} 43 constexpr void VoidReturn(); // expected-error {{constexpr function's return type 'void' is not a literal type}} 44 constexpr ~T(); // expected-error {{destructor cannot be marked constexpr}} 45 typedef NonLiteral F(); 46 constexpr F NonLiteralReturn2; // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}} 47 48 // - each of its parameter types shall be a literal type; 49 constexpr int NonLiteralParam(NonLiteral); // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}} 50 typedef int G(NonLiteral); 51 constexpr G NonLiteralParam2; // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}} 52 53 // - its function-body shall be = delete, = default, 54 constexpr int Deleted() = delete; 55 // It's not possible for the function-body to legally be "= default" here. 56 // Other than constructors, only the copy- and move-assignment operators and 57 // destructor can be defaulted. Destructors can't be constexpr since they 58 // don't have a literal return type. Defaulted assignment operators can't be 59 // constexpr since they can't be const. 60 constexpr T &operator=(const T&) = default; // expected-error {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}} 61}; 62struct U { 63 constexpr U SelfReturn(); 64 constexpr int SelfParam(U); 65}; 66 67struct V : virtual U { // expected-note {{here}} 68 constexpr int F(); // expected-error {{constexpr member function not allowed in struct with virtual base class}} 69}; 70 71// or a compound-statememt that contains only 72constexpr int AllowedStmts() { 73 // - null statements 74 ; 75 76 // - static_assert-declarations 77 static_assert(true, "the impossible happened!"); 78 79 // - typedef declarations and alias-declarations that do not define classes 80 // or enumerations 81 typedef int I; 82 typedef struct S T; 83 using J = int; 84 using K = int[sizeof(I) + sizeof(J)]; 85 // Note, the standard requires we reject this. 86 struct U; 87 88 // - using-declarations 89 using N::C; 90 91 // - using-directives 92 using namespace N; 93 94 // - and exactly one return statement 95 return sizeof(K) + sizeof(C) + sizeof(K); 96} 97constexpr int ForStmt() { 98 for (int n = 0; n < 10; ++n) // expected-error {{statement not allowed in constexpr function}} 99 return 0; 100} 101constexpr int VarDecl() { 102 constexpr int a = 0; // expected-error {{variables cannot be declared in a constexpr function}} 103 return 0; 104} 105constexpr int FuncDecl() { 106 constexpr int ForwardDecl(int); // expected-error {{statement not allowed in constexpr function}} 107 return ForwardDecl(42); 108} 109constexpr int ClassDecl1() { 110 typedef struct { } S1; // expected-error {{types cannot be defined in a constexpr function}} 111 return 0; 112} 113constexpr int ClassDecl2() { 114 using S2 = struct { }; // expected-error {{types cannot be defined in a constexpr function}} 115 return 0; 116} 117constexpr int ClassDecl3() { 118 struct S3 { }; // expected-error {{types cannot be defined in a constexpr function}} 119 return 0; 120} 121constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}} 122constexpr int MultiReturn() { 123 return 0; // expected-note {{return statement}} 124 return 0; // expected-error {{multiple return statements in constexpr function}} 125} 126 127// - every constructor call and implicit conversion used in initializing the 128// return value shall be one of those allowed in a constant expression. 129// 130// We implement the proposed resolution of DR1364 and ignore this bullet. 131// However, we implement the spirit of the check as part of the p5 checking that 132// a constexpr function must be able to produce a constant expression. 133namespace DR1364 { 134 constexpr int f(int k) { 135 return k; // ok, even though lvalue-to-rvalue conversion of a function 136 // parameter is not allowed in a constant expression. 137 } 138 int kGlobal; // expected-note {{here}} 139 constexpr int f() { // expected-error {{constexpr function never produces a constant expression}} 140 return kGlobal; // expected-note {{read of non-const}} 141 } 142} 143