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