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 { 28c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith constexpr int mi1; // expected-error {{non-static data member cannot be constexpr}} 29c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smith static constexpr int mi2; // expected-error {{requires an initializer}} 30737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 31af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// typedef 32af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithtypedef constexpr int CI; // expected-error {{typedef cannot be constexpr}} 33af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// tag 34af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr class C1 {}; // expected-error {{class cannot be marked constexpr}} 35af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}} 36af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr union U1 {}; // expected-error {{union cannot be marked constexpr}} 37af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}} 380661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}} 390661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}} 400661bd0ccae381613c5967cdf2514255e1f92636Peter Collingbournetemplate <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}} 41af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithclass C2 {} constexpr; // expected-error {{class cannot be marked constexpr}} 42af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithstruct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}} 43af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithunion U2 {} constexpr; // expected-error {{union cannot be marked constexpr}} 44af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithenum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}} 456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithconstexpr class C3 {} c3 = C3(); 466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithconstexpr struct S3 {} s3 = S3(); 47af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr union U3 {} u3 = {}; 48af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr enum E3 { V3 } e3 = V3; 496180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithclass C4 {} constexpr c4 = C4(); 506180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstruct S4 {} constexpr s4 = S4(); 51af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithunion U4 {} constexpr u4 = {}; 52af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithenum E4 { V4 } constexpr e4 = V4; 53af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr int; // expected-error {{constexpr can only be used in variable and function declarations}} 54737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// redeclaration mismatch 559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int f3(); // expected-note {{previous declaration is here}} 569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithint f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}} 579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithint f4(); // expected-note {{previous declaration is here}} 589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}} 599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f5(T); 609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f5(T); // expected-note {{previous}} 619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}} 629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> T f6(T); // expected-note {{here}} 639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}} 64af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith// destructor 65af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithstruct ConstexprDtor { 66af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith constexpr ~ConstexprDtor() = default; // expected-error {{destructor cannot be marked constexpr}} 67af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith}; 68737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 69737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// template stuff 701d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate <typename T> constexpr T ft(T t) { return t; } 719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <typename T> T gt(T t) { return t; } 729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct S { 739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith template<typename T> constexpr T f(); 749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith template<typename T> T g() const; 759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 76737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// explicit specialization can differ in constepxr 78ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> notlit ft(notlit nl) { return nl; } 79ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> char ft(char c) { return c; } // expected-note {{previous}} 80ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}} 81ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> constexpr int gt(int nl) { return nl; } 82ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> notlit S::f() const { return notlit(); } 83ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> constexpr int S::g() { return 0; } // expected-note {{previous}} 84ff2348888133dcc64f7363af2093cb608caeb7ceRichard Smithtemplate <> int S::g() const; // expected-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}} 859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// specializations can drop the 'constexpr' but not the implied 'const'. 869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <> char S::g() { return 0; } // expected-error {{no function template matches}} 879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate <> double S::g() const { return 0; } // ok 88737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 891d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithconstexpr int i3 = ft(1); 90737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 91737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlvoid test() { 92737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl // ignore constexpr when instantiating with non-literal 93737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl notlit2 nl2; 94737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl (void)ft(nl2); 95737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 96737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 97737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl// Examples from the standard: 98099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr int square(int x); // expected-note {{declared here}} 99737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int bufsz = 1024; 100737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 101af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithconstexpr struct pixel { // expected-error {{struct cannot be marked constexpr}} 102737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl int x; 103737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl int y; 104737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl constexpr pixel(int); 105737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl}; 106737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 107737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr pixel::pixel(int a) 108099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}} 109737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl { } 110737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 111099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}} 112737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 113737801257f795632175517ffce4a80c62fc7bff7Sebastian Redlconstexpr int square(int x) { 114737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl return x * x; 115737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 116737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 117180f47959a066795cc0f409433023af448bb0328Richard Smithconstexpr pixel large(4); 118737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 119af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smithint next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}} 120737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl return x + 1; 121737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl} 122737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl 123c6d990a767150b02337de1136fdb55ccf349f4d1Richard Smithextern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}} 124