p1.cpp revision d9bf41893c3a59d5ea33e7b04b0e6211f7dca201
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 2 3struct notlit { // expected-note {{not literal because}} 4 notlit() {} 5}; 6struct notlit2 { 7 notlit2() {} 8}; 9 10// valid declarations 11constexpr int i1 = 0; 12constexpr int f1() { return 0; } 13struct s1 { 14 constexpr static int mi1 = 0; 15 const static int mi2; 16}; 17constexpr int s1::mi2 = 0; 18 19// invalid declarations 20// not a definition of an object 21constexpr extern int i2; // expected-error {{constexpr variable declaration must be a definition}} 22// not a literal type 23constexpr notlit nl1; // expected-error {{constexpr variable cannot have non-literal type 'const notlit'}} 24// function parameters 25void f2(constexpr int i) {} // expected-error {{function parameter cannot be constexpr}} 26// non-static member 27struct s2 { 28 constexpr int mi1; // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}} 29 static constexpr int mi2; // expected-error {{requires an initializer}} 30 mutable constexpr int mi3 = 3; // expected-error-re {{non-static data member cannot be constexpr$}} expected-error {{'mutable' and 'const' cannot be mixed}} 31}; 32// typedef 33typedef constexpr int CI; // expected-error {{typedef cannot be constexpr}} 34// tag 35constexpr class C1 {}; // expected-error {{class cannot be marked constexpr}} 36constexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}} 37constexpr union U1 {}; // expected-error {{union cannot be marked constexpr}} 38constexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}} 39template <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}} 40template <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}} 41template <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}} 42class C2 {} constexpr; // expected-error {{class cannot be marked constexpr}} 43struct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}} 44union U2 {} constexpr; // expected-error {{union cannot be marked constexpr}} 45enum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}} 46constexpr class C3 {} c3 = C3(); 47constexpr struct S3 {} s3 = S3(); 48constexpr union U3 {} u3 = {}; 49constexpr enum E3 { V3 } e3 = V3; 50class C4 {} constexpr c4 = C4(); 51struct S4 {} constexpr s4 = S4(); 52union U4 {} constexpr u4 = {}; 53enum E4 { V4 } constexpr e4 = V4; 54constexpr int; // expected-error {{constexpr can only be used in variable and function declarations}} 55// redeclaration mismatch 56constexpr int f3(); // expected-note {{previous declaration is here}} 57int f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}} 58int f4(); // expected-note {{previous declaration is here}} 59constexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}} 60template<typename T> constexpr T f5(T); 61template<typename T> constexpr T f5(T); // expected-note {{previous}} 62template<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}} 63template<typename T> T f6(T); // expected-note {{here}} 64template<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}} 65// destructor 66struct ConstexprDtor { 67 constexpr ~ConstexprDtor() = default; // expected-error {{destructor cannot be marked constexpr}} 68}; 69 70// template stuff 71template <typename T> constexpr T ft(T t) { return t; } 72template <typename T> T gt(T t) { return t; } 73struct S { 74 template<typename T> constexpr T f(); 75 template<typename T> T g() const; 76}; 77 78// explicit specialization can differ in constepxr 79template <> notlit ft(notlit nl) { return nl; } 80template <> char ft(char c) { return c; } // expected-note {{previous}} 81template <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}} 82template <> constexpr int gt(int nl) { return nl; } 83template <> notlit S::f() const { return notlit(); } 84template <> constexpr int S::g() { return 0; } // expected-note {{previous}} 85template <> int S::g() const; // expected-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}} 86// specializations can drop the 'constexpr' but not the implied 'const'. 87template <> char S::g() { return 0; } // expected-error {{no function template matches}} 88template <> double S::g() const { return 0; } // ok 89 90constexpr int i3 = ft(1); 91 92void test() { 93 // ignore constexpr when instantiating with non-literal 94 notlit2 nl2; 95 (void)ft(nl2); 96} 97 98// Examples from the standard: 99constexpr int square(int x); // expected-note {{declared here}} 100constexpr int bufsz = 1024; 101 102constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}} 103 int x; 104 int y; 105 constexpr pixel(int); 106}; 107 108constexpr pixel::pixel(int a) 109 : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}} 110 { } 111 112constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}} 113 114constexpr int square(int x) { 115 return x * x; 116} 117 118constexpr pixel large(4); 119 120int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}} 121 return x + 1; 122} 123 124extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}} 125