1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 2737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 386c3ae46250cdcc57778c27826060779a92f3815Richard Smithstruct notlit { // expected-note {{not literal because}} 4737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl notlit() {} 5737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 6737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlstruct notlit2 { 7737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl notlit2() {} 8737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 9737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 10737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// valid declarations 11737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int i1 = 0; 12737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int f1() { return 0; } 13737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlstruct s1 { 14c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith constexpr static int mi1 = 0; 15c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith const static int mi2; 16737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 17c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smithconstexpr int s1::mi2 = 0; 18737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 19737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// invalid declarations 20737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// not a definition of an object 21c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smithconstexpr extern int i2; // expected-error {{constexpr variable declaration must be a definition}} 22737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// not a literal type 2386c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr notlit nl1; // expected-error {{constexpr variable cannot have non-literal type 'const notlit'}} 24737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// function parameters 25af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithvoid f2(constexpr int i) {} // expected-error {{function parameter cannot be constexpr}} 26737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// non-static member 27737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlstruct s2 { 281d87fbaeea4a9fbbd73b3a53641f59f1673098e5David Blaikie constexpr int mi1; // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}} 29c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith static constexpr int mi2; // expected-error {{requires an initializer}} 30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines mutable constexpr int mi3 = 3; // expected-error-re {{non-static data member cannot be constexpr{{$}}}} expected-error {{'mutable' and 'const' cannot be mixed}} 31737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 32af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// typedef 33af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithtypedef constexpr int CI; // expected-error {{typedef cannot be constexpr}} 34af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// tag 35af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr class C1 {}; // expected-error {{class cannot be marked constexpr}} 36af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}} 37af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr union U1 {}; // expected-error {{union cannot be marked constexpr}} 38af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}} 390661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}} 400661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}} 410661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}} 42af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithclass C2 {} constexpr; // expected-error {{class cannot be marked constexpr}} 43af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithstruct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}} 44af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithunion U2 {} constexpr; // expected-error {{union cannot be marked constexpr}} 45af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithenum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}} 466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithconstexpr class C3 {} c3 = C3(); 476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithconstexpr struct S3 {} s3 = S3(); 48af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr union U3 {} u3 = {}; 49af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr enum E3 { V3 } e3 = V3; 506180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithclass C4 {} constexpr c4 = C4(); 516180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstruct S4 {} constexpr s4 = S4(); 52af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithunion U4 {} constexpr u4 = {}; 53af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithenum E4 { V4 } constexpr e4 = V4; 54af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr int; // expected-error {{constexpr can only be used in variable and function declarations}} 55737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// redeclaration mismatch 569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int f3(); // expected-note {{previous declaration is here}} 579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithint f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}} 589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithint f4(); // expected-note {{previous declaration is here}} 599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}} 609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f5(T); 619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f5(T); // expected-note {{previous}} 629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}} 639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> T f6(T); // expected-note {{here}} 649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}} 65af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// destructor 66af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithstruct ConstexprDtor { 67af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith constexpr ~ConstexprDtor() = default; // expected-error {{destructor cannot be marked constexpr}} 68af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith}; 69737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 70737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// template stuff 711d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate <typename T> constexpr T ft(T t) { return t; } 729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <typename T> T gt(T t) { return t; } 739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct S { 74840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith template<typename T> constexpr T f(); // expected-warning {{C++1y}} 754384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufo template <typename T> 76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines T g() const; // expected-note-re {{candidate template ignored: could not match 'T (){{( __attribute__\(\(thiscall\)\))?}} const' against 'char (){{( __attribute__\(\(thiscall\)\))?}}'}} 779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 78737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 799f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// explicit specialization can differ in constepxr 80ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> notlit ft(notlit nl) { return nl; } 81ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> char ft(char c) { return c; } // expected-note {{previous}} 82ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}} 83ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> constexpr int gt(int nl) { return nl; } 84ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> notlit S::f() const { return notlit(); } 85840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smithtemplate <> constexpr int S::g() { return 0; } // expected-note {{previous}} expected-warning {{C++1y}} 86ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> int S::g() const; // expected-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}} 879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// specializations can drop the 'constexpr' but not the implied 'const'. 889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <> char S::g() { return 0; } // expected-error {{no function template matches}} 899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <> double S::g() const { return 0; } // ok 90737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 911d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithconstexpr int i3 = ft(1); 92737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 93737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlvoid test() { 94737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl // ignore constexpr when instantiating with non-literal 95737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl notlit2 nl2; 96737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl (void)ft(nl2); 97737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 98737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 99737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// Examples from the standard: 100099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr int square(int x); // expected-note {{declared here}} 101737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int bufsz = 1024; 102737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 103af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr struct pixel { // expected-error {{struct cannot be marked constexpr}} 104737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl int x; 105737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl int y; 106737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl constexpr pixel(int); 107737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 108737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 109737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr pixel::pixel(int a) 110099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}} 111737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl { } 112737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 113099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}} 114737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 115737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int square(int x) { 116737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl return x * x; 117737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 118737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 119180f47959a066795cc0f409433023af448bb0328Richard Smithconstexpr pixel large(4); 120737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 121af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithint next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}} 122737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl return x + 1; 123737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 124737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 125c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smithextern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}} 126