p1.cpp revision 86c3ae46250cdcc57778c27826060779a92f3815
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2
3struct notlit { // expected-note {{not literal because}}
4  notlit() {}
5};
6struct notlit2 {
7  notlit2() {}
8};
9
10// valid declarations
11constexpr int i1 = 0;
12constexpr int f1() { return 0; }
13struct s1 {
14  constexpr static int mi1 = 0;
15  const static int mi2;
16};
17constexpr int s1::mi2 = 0;
18
19// invalid declarations
20// not a definition of an object
21constexpr extern int i2; // expected-error {{constexpr variable declaration must be a definition}}
22// not a literal type
23constexpr notlit nl1; // expected-error {{constexpr variable cannot have non-literal type 'const notlit'}}
24// function parameters
25void f2(constexpr int i) {} // expected-error {{function parameter cannot be constexpr}}
26// non-static member
27struct s2 {
28  constexpr int mi1; // expected-error {{non-static data member cannot be constexpr}}
29  static constexpr int mi2; // expected-error {{requires an initializer}}
30};
31// typedef
32typedef constexpr int CI; // expected-error {{typedef cannot be constexpr}}
33// tag
34constexpr class C1 {}; // expected-error {{class cannot be marked constexpr}}
35constexpr struct S1 {}; // expected-error {{struct cannot be marked constexpr}}
36constexpr union U1 {}; // expected-error {{union cannot be marked constexpr}}
37constexpr enum E1 {}; // expected-error {{enum cannot be marked constexpr}}
38template <typename T> constexpr class TC1 {}; // expected-error {{class cannot be marked constexpr}}
39template <typename T> constexpr struct TS1 {}; // expected-error {{struct cannot be marked constexpr}}
40template <typename T> constexpr union TU1 {}; // expected-error {{union cannot be marked constexpr}}
41class C2 {} constexpr; // expected-error {{class cannot be marked constexpr}}
42struct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}}
43union U2 {} constexpr; // expected-error {{union cannot be marked constexpr}}
44enum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}}
45constexpr class C3 {} c3 = C3();
46constexpr struct S3 {} s3 = S3();
47constexpr union U3 {} u3 = {};
48constexpr enum E3 { V3 } e3 = V3;
49class C4 {} constexpr c4 = C4();
50struct S4 {} constexpr s4 = S4();
51union U4 {} constexpr u4 = {};
52enum E4 { V4 } constexpr e4 = V4;
53constexpr int; // expected-error {{constexpr can only be used in variable and function declarations}}
54// redeclaration mismatch
55constexpr int f3(); // expected-note {{previous declaration is here}}
56int f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}}
57int f4(); // expected-note {{previous declaration is here}}
58constexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}}
59template<typename T> constexpr T f5(T);
60template<typename T> constexpr T f5(T); // expected-note {{previous}}
61template<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}}
62template<typename T> T f6(T); // expected-note {{here}}
63template<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}}
64// destructor
65struct ConstexprDtor {
66  constexpr ~ConstexprDtor() = default; // expected-error {{destructor cannot be marked constexpr}}
67};
68
69// template stuff
70template <typename T> constexpr T ft(T t) { return t; }
71template <typename T> T gt(T t) { return t; }
72struct S {
73  template<typename T> constexpr T f();
74  template<typename T> T g() const;
75};
76
77// explicit specialization can differ in constepxr
78// FIXME: When checking the explicit specialization, we implicitly instantiate
79// the primary template then claim a constexpr mismatch.
80template <> notlit ft(notlit nl) { return nl; } // unexpected-error {{follows constexpr declaration}} unexpected-note {{here}}
81template <> char ft(char c) { return c; } // desired-note {{previous}} unexpected-error {{follows constexpr declaration}} unexpected-note {{here}}
82template <> constexpr char ft(char nl); // desired-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}}
83template <> constexpr int gt(int nl) { return nl; } // unexpected-error {{follows non-constexpr declaration}} unexpected-note {{here}}
84template <> notlit S::f() const { return notlit(); } // unexpected-error {{follows constexpr declaration}} unexpected-note {{here}}
85template <> constexpr int S::g() { return 0; } // desired-note {{previous}} unexpected-error {{follows non-constexpr declaration}} unexpected-note {{here}}
86template <> int S::g() const; // desired-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}}
87// specializations can drop the 'constexpr' but not the implied 'const'.
88template <> char S::g() { return 0; } // expected-error {{no function template matches}}
89template <> double S::g() const { return 0; } // ok
90
91constexpr int i3 = ft(1);
92
93void test() {
94  // ignore constexpr when instantiating with non-literal
95  notlit2 nl2;
96  (void)ft(nl2);
97}
98
99// Examples from the standard:
100constexpr int square(int x); // expected-note {{declared here}}
101constexpr int bufsz = 1024;
102
103constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}}
104  int x;
105  int y;
106  constexpr pixel(int);
107};
108
109constexpr pixel::pixel(int a)
110  : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}}
111  { }
112
113constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}}
114
115constexpr int square(int x) {
116  return x * x;
117}
118
119constexpr pixel large(4);
120
121int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}}
122      return x + 1;
123}
124
125extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}}
126