1 // RUN: %clang_cc1 -verify -std=c++11 %s 2 3 namespace N { 4 typedef char C; 5 } 6 7 namespace M { 8 typedef double D; 9 } 10 11 struct NonLiteral { // expected-note 4{{no constexpr constructors}} 12 NonLiteral() {} 13 NonLiteral(int) {} 14 }; 15 struct Literal { 16 constexpr Literal() {} 17 operator int() const { return 0; } 18 }; 19 20 struct S { 21 virtual int ImplicitlyVirtual() const = 0; // expected-note {{overridden virtual function}} 22 }; 23 struct 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: 33 struct T : SS { // expected-note {{base class 'SS' 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 ~T(); // expected-error {{destructor cannot be marked constexpr}} 44 typedef NonLiteral F(); 45 constexpr F NonLiteralReturn2; // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}} 46 47 // - each of its parameter types shall be a literal type; 48 constexpr int NonLiteralParam(NonLiteral); // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}} 49 typedef int G(NonLiteral); 50 constexpr G NonLiteralParam2; // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}} 51 52 // - its function-body shall be = delete, = default, 53 constexpr int Deleted() = delete; 54 // It's not possible for the function-body to legally be "= default" here. 55 // Other than constructors, only the copy- and move-assignment operators and 56 // destructor can be defaulted. Destructors can't be constexpr since they 57 // don't have a literal return type. Defaulted assignment operators can't be 58 // constexpr since they can't be const. 59 constexpr T &operator=(const T&) = default; // expected-error {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}} 60 }; 61 struct U { 62 constexpr U SelfReturn(); 63 constexpr int SelfParam(U); 64 }; 65 66 // or a compound-statememt that contains only 67 constexpr int AllowedStmts() { 68 // - null statements 69 ; 70 71 // - static_assert-declarations 72 static_assert(true, "the impossible happened!"); 73 74 // - typedef declarations and alias-declarations that do not define classes 75 // or enumerations 76 typedef int I; 77 typedef struct S T; 78 using J = int; 79 using K = int[sizeof(I) + sizeof(J)]; 80 // Note, the standard requires we reject this. 81 struct U; 82 83 // - using-declarations 84 using N::C; 85 86 // - using-directives 87 using namespace N; 88 89 // - and exactly one return statement 90 return sizeof(K) + sizeof(C) + sizeof(K); 91 } 92 constexpr int ForStmt() { 93 for (int n = 0; n < 10; ++n) // expected-error {{statement not allowed in constexpr function}} 94 return 0; 95 } 96 constexpr int VarDecl() { 97 constexpr int a = 0; // expected-error {{variables cannot be declared in a constexpr function}} 98 return 0; 99 } 100 constexpr int FuncDecl() { 101 constexpr int ForwardDecl(int); // expected-error {{statement not allowed in constexpr function}} 102 return ForwardDecl(42); 103 } 104 constexpr int ClassDecl1() { 105 typedef struct { } S1; // expected-error {{types cannot be defined in a constexpr function}} 106 return 0; 107 } 108 constexpr int ClassDecl2() { 109 using S2 = struct { }; // expected-error {{types cannot be defined in a constexpr function}} 110 return 0; 111 } 112 constexpr int ClassDecl3() { 113 struct S3 { }; // expected-error {{types cannot be defined in a constexpr function}} 114 return 0; 115 } 116 constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}} 117 constexpr int MultiReturn() { 118 return 0; // expected-note {{return statement}} 119 return 0; // expected-error {{multiple return statements in constexpr function}} 120 } 121 122 // - every constructor call and implicit conversion used in initializing the 123 // return value shall be one of those allowed in a constant expression. 124 // 125 // We implement the proposed resolution of DR1364 and ignore this bullet. 126