176 lines · cpp
1// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++11 %s2// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++14 %s3// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++17 %s4// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++2a %s5 6// MSVC always adopted the C++17 rule that implies that constexpr variables are7// implicitly inline, so do the test again.8// RUN: %clang_cc1 -triple x86_64-windows-msvc -DMS_ABI -fsyntax-only -verify -std=c++11 %s9 10struct notlit { // expected-note {{not literal because}}11 notlit() {}12};13struct notlit2 {14 notlit2() {}15};16 17// valid declarations18constexpr int i1 = 0;19constexpr int f1() { return 0; }20struct s1 {21 constexpr static int mi1 = 0;22 const static int mi2;23};24constexpr int s1::mi2 = 0;25 26// invalid declarations27// not a definition of an object28constexpr extern int i2; // expected-error {{constexpr variable declaration must be a definition}}29// not a literal type30constexpr notlit nl1; // expected-error {{constexpr variable cannot have non-literal type 'const notlit'}}31// function parameters32void f2(constexpr int i) {} // expected-error {{function parameter cannot be constexpr}}33// non-static member34struct s2 {35 constexpr int mi1; // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}}36 static constexpr int mi2;37#if __cplusplus <= 201402L && !defined(MS_ABI)38 // expected-error@-2 {{requires an initializer}}39#else40 // expected-error@-4 {{constexpr variable 'mi2' must be initialized by a constant expression}}41#endif42 mutable constexpr int mi3 = 3; // expected-error-re {{non-static data member cannot be constexpr{{$}}}} expected-error {{'mutable' and 'const' cannot be mixed}}43};44// typedef45typedef constexpr int CI; // expected-error {{typedef cannot be constexpr}}46// tag47constexpr class C1 {}; // expected-error {{class cannot be marked constexpr}}48constexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}}49constexpr union U1 {}; // expected-error {{union cannot be marked constexpr}}50constexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}}51template <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}}52template <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}}53template <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}}54class C2 {} constexpr; // expected-error {{class cannot be marked constexpr}}55struct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}}56union U2 {} constexpr; // expected-error {{union cannot be marked constexpr}}57enum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}}58constexpr class C3 {} c3 = C3();59constexpr struct S3 {} s3 = S3();60constexpr union U3 {} u3 = {};61constexpr enum E3 { V3 } e3 = V3;62class C4 {} constexpr c4 = C4();63struct S4 {} constexpr s4 = S4();64union U4 {} constexpr u4 = {};65enum E4 { V4 } constexpr e4 = V4;66constexpr int; // expected-error {{constexpr can only be used in variable and function declarations}}67// redeclaration mismatch68constexpr int f3(); // expected-note {{previous declaration is here}}69int f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}}70int f4(); // expected-note {{previous declaration is here}}71constexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}}72template<typename T> constexpr T f5(T);73template<typename T> constexpr T f5(T); // expected-note {{previous}}74template<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}}75template<typename T> T f6(T); // expected-note {{here}}76template<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}}77// destructor78struct ConstexprDtor {79 constexpr ~ConstexprDtor() = default;80#if __cplusplus <= 201703L81 // expected-error@-2 {{destructor cannot be declared constexpr}}82#endif83};84 85// template stuff86template <typename T> constexpr T ft(T t) { return t; }87template <typename T> T gt(T t) { return t; }88struct S {89 template<typename T> constexpr T f(); // expected-warning 0-1{{C++14}} expected-note 0-1{{candidate}}90 template <typename T>91 T g() const; // expected-note-re {{candidate template ignored: could not match 'T (){{( __attribute__\(\(thiscall\)\))?}} const' against 'char (){{( __attribute__\(\(thiscall\)\))?}}'}}92#if __cplusplus >= 201402L93 // expected-note@-2 {{candidate template ignored: could not match 'T () const' against 'int ()'}}94#endif95};96 97// explicit specialization can differ in constepxr98template <> notlit ft(notlit nl) { return nl; }99template <> char ft(char c) { return c; } // expected-note {{previous}}100template <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}}101template <> constexpr int gt(int nl) { return nl; }102template <> notlit S::f() const { return notlit(); }103#if __cplusplus >= 201402L104// expected-error@-2 {{no function template matches}}105#endif106template <> constexpr int S::g() { return 0; }107#if __cplusplus < 201402L108// expected-warning@-2 {{C++14}}109// expected-note@-3 {{previous}}110#else111// expected-error@-5 {{no function template matches function template specialization 'g'}}112#endif113template <> int S::g() const;114#if __cplusplus < 201402L115// expected-error@-2 {{non-constexpr declaration of 'g<int>' follows constexpr declaration}}116#endif117// specializations can drop the 'constexpr' but not the implied 'const'.118template <> char S::g() { return 0; } // expected-error {{no function template matches}}119template <> double S::g() const { return 0; } // ok120 121constexpr int i3 = ft(1);122 123void test() {124 // ignore constexpr when instantiating with non-literal125 notlit2 nl2;126 (void)ft(nl2);127}128 129// Examples from the standard:130constexpr int square(int x); // expected-note {{declared here}}131constexpr int bufsz = 1024;132 133constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}}134 int x;135 int y;136 constexpr pixel(int);137};138 139constexpr pixel::pixel(int a)140 : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}}141 { }142 143constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}}144 145constexpr int square(int x) {146 return x * x;147}148 149constexpr pixel large(4);150 151int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}}152 return x + 1;153}154 155extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}}156 157namespace {158 struct A {159 static constexpr int n = 0;160 };161 // FIXME: We should diagnose this prior to C++17.162 const int &r = A::n;163}164 165#if __cplusplus < 201402L166namespace ImplicitConstexprDef {167 struct A { // #defined-here168 void f(); // expected-note {{member declaration does not match because it is not const qualified}}169 };170 171 constexpr void A::f() { } // expected-warning {{'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior}}172 // expected-error@-1 {{out-of-line definition of 'f' does not match any declaration in 'ImplicitConstexprDef::A'}}173 // expected-note@#defined-here {{defined here}}174}175#endif176