• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++14 %s
3 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++17 %s
4 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++2a %s
5 
6 // MSVC always adopted the C++17 rule that implies that constexpr variables are
7 // implicitly inline, so do the test again.
8 // RUN: %clang_cc1 -triple x86_64-windows-msvc -DMS_ABI -fsyntax-only -verify -std=c++11 %s
9 
10 struct notlit { // expected-note {{not literal because}}
notlitnotlit11   notlit() {}
12 };
13 struct notlit2 {
notlit2notlit214   notlit2() {}
15 };
16 
17 // valid declarations
18 constexpr int i1 = 0;
f1()19 constexpr int f1() { return 0; }
20 struct s1 {
21   constexpr static int mi1 = 0;
22   const static int mi2;
23 };
24 constexpr int s1::mi2 = 0;
25 
26 // invalid declarations
27 // not a definition of an object
28 constexpr extern int i2; // expected-error {{constexpr variable declaration must be a definition}}
29 // not a literal type
30 constexpr notlit nl1; // expected-error {{constexpr variable cannot have non-literal type 'const notlit'}}
31 // function parameters
f2(constexpr int i)32 void f2(constexpr int i) {} // expected-error {{function parameter cannot be constexpr}}
33 // non-static member
34 struct 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 #else
40   // expected-error@-4 {{default initialization of an object of const}}
41 #endif
42   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 // typedef
45 typedef constexpr int CI; // expected-error {{typedef cannot be constexpr}}
46 // tag
47 constexpr class C1 {}; // expected-error {{class cannot be marked constexpr}}
48 constexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}}
49 constexpr union U1 {}; // expected-error {{union cannot be marked constexpr}}
50 constexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}}
51 template <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}}
52 template <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}}
53 template <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}}
54 class C2 {} constexpr; // expected-error {{class cannot be marked constexpr}}
55 struct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}}
56 union U2 {} constexpr; // expected-error {{union cannot be marked constexpr}}
57 enum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}}
58 constexpr class C3 {} c3 = C3();
59 constexpr struct S3 {} s3 = S3();
60 constexpr union U3 {} u3 = {};
61 constexpr enum E3 { V3 } e3 = V3;
62 class C4 {} constexpr c4 = C4();
63 struct S4 {} constexpr s4 = S4();
64 union U4 {} constexpr u4 = {};
65 enum E4 { V4 } constexpr e4 = V4;
66 constexpr int; // expected-error {{constexpr can only be used in variable and function declarations}}
67 // redeclaration mismatch
68 constexpr int f3(); // expected-note {{previous declaration is here}}
69 int f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}}
70 int f4(); // expected-note {{previous declaration is here}}
71 constexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}}
72 template<typename T> constexpr T f5(T);
73 template<typename T> constexpr T f5(T); // expected-note {{previous}}
74 template<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}}
75 template<typename T> T f6(T); // expected-note {{here}}
76 template<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}}
77 // destructor
78 struct ConstexprDtor {
79   constexpr ~ConstexprDtor() = default;
80 #if __cplusplus <= 201703L
81   // expected-error@-2 {{destructor cannot be declared constexpr}}
82 #endif
83 };
84 
85 // template stuff
ft(T t)86 template <typename T> constexpr T ft(T t) { return t; }
gt(T t)87 template <typename T> T gt(T t) { return t; }
88 struct 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 };
93 
94 // explicit specialization can differ in constepxr
ft(notlit nl)95 template <> notlit ft(notlit nl) { return nl; }
ft(char c)96 template <> char ft(char c) { return c; } // expected-note {{previous}}
97 template <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}}
gt(int nl)98 template <> constexpr int gt(int nl) { return nl; }
f() const99 template <> notlit S::f() const { return notlit(); }
100 #if __cplusplus >= 201402L
101 // expected-error@-2 {{no function template matches}}
102 #endif
g()103 template <> constexpr int S::g() { return 0; } // expected-note {{previous}}
104 #if __cplusplus < 201402L
105 // expected-warning@-2 {{C++14}}
106 #else
107 // expected-error@-4 {{does not match any declaration in 'S'}}
108 #endif
109 template <> int S::g() const; // expected-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}}
110 // specializations can drop the 'constexpr' but not the implied 'const'.
g()111 template <> char S::g() { return 0; } // expected-error {{no function template matches}}
g() const112 template <> double S::g() const { return 0; } // ok
113 
114 constexpr int i3 = ft(1);
115 
test()116 void test() {
117   // ignore constexpr when instantiating with non-literal
118   notlit2 nl2;
119   (void)ft(nl2);
120 }
121 
122 // Examples from the standard:
123 constexpr int square(int x); // expected-note {{declared here}}
124 constexpr int bufsz = 1024;
125 
126 constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}}
127   int x;
128   int y;
129   constexpr pixel(int);
130 };
131 
pixel(int a)132 constexpr pixel::pixel(int a)
133   : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}}
134   { }
135 
136 constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}}
137 
square(int x)138 constexpr int square(int x) {
139   return x * x;
140 }
141 
142 constexpr pixel large(4);
143 
next(constexpr int x)144 int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}}
145       return x + 1;
146 }
147 
148 extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}}
149 
150 namespace {
151   struct A {
152     static constexpr int n = 0;
153   };
154   // FIXME: We should diagnose this prior to C++17.
155   const int &r = A::n;
156 }
157