p1.cpp revision 9f569cca2a4c5fb6026005434e27025b9e71309d
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s
2
3struct notlit {
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 {{declaration of constexpr variable 'nl1' requires an initializer}}
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}}
38class C2 {} constexpr; // expected-error {{class cannot be marked constexpr}}
39struct S2 {} constexpr; // expected-error {{struct cannot be marked constexpr}}
40union U2 {} constexpr; // expected-error {{union cannot be marked constexpr}}
41enum E2 {} constexpr; // expected-error {{enum cannot be marked constexpr}}
42constexpr class C3 {} c3 = C3();
43constexpr struct S3 {} s3 = S3();
44constexpr union U3 {} u3 = {};
45constexpr enum E3 { V3 } e3 = V3;
46class C4 {} constexpr c4 = C4();
47struct S4 {} constexpr s4 = S4();
48union U4 {} constexpr u4 = {};
49enum E4 { V4 } constexpr e4 = V4;
50constexpr int; // expected-error {{constexpr can only be used in variable and function declarations}}
51// redeclaration mismatch
52constexpr int f3(); // expected-note {{previous declaration is here}}
53int f3(); // expected-error {{non-constexpr declaration of 'f3' follows constexpr declaration}}
54int f4(); // expected-note {{previous declaration is here}}
55constexpr int f4(); // expected-error {{constexpr declaration of 'f4' follows non-constexpr declaration}}
56template<typename T> constexpr T f5(T);
57template<typename T> constexpr T f5(T); // expected-note {{previous}}
58template<typename T> T f5(T); // expected-error {{non-constexpr declaration of 'f5' follows constexpr declaration}}
59template<typename T> T f6(T); // expected-note {{here}}
60template<typename T> constexpr T f6(T); // expected-error {{constexpr declaration of 'f6' follows non-constexpr declaration}}
61// destructor
62struct ConstexprDtor {
63  constexpr ~ConstexprDtor() = default; // expected-error {{destructor cannot be marked constexpr}}
64};
65
66// template stuff
67template <typename T> constexpr T ft(T t) { return t; }
68template <typename T> T gt(T t) { return t; }
69struct S {
70  template<typename T> constexpr T f();
71  template<typename T> T g() const;
72};
73
74// explicit specialization can differ in constepxr
75// FIXME: When checking the explicit specialization, we implicitly instantiate
76// the primary template then claim a constexpr mismatch.
77template <> notlit ft(notlit nl) { return nl; }
78template <> char ft(char c) { return c; } // desired-note {{previous}} unexpected-error {{follows constexpr declaration}} unexpected-note {{here}}
79template <> constexpr char ft(char nl); // desired-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}}
80template <> constexpr int gt(int nl) { return nl; } // unexpected-error {{follows non-constexpr declaration}} unexpected-note {{here}}
81template <> notlit S::f() const { return notlit(); }
82template <> constexpr int S::g() { return 0; } // desired-note {{previous}} unexpected-error {{follows non-constexpr declaration}} unexpected-note {{here}}
83template <> int S::g() const; // desired-error {{non-constexpr declaration of 'g<int>' follows constexpr declaration}}
84// specializations can drop the 'constexpr' but not the implied 'const'.
85template <> char S::g() { return 0; } // expected-error {{no function template matches}}
86template <> double S::g() const { return 0; } // ok
87
88// FIXME: The initializer is a constant expression.
89constexpr int i3 = ft(1); // unexpected-error {{must be initialized by a constant expression}}
90
91void test() {
92  // ignore constexpr when instantiating with non-literal
93  notlit2 nl2;
94  (void)ft(nl2);
95}
96
97// Examples from the standard:
98constexpr int square(int x);
99constexpr int bufsz = 1024;
100
101constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}}
102  int x;
103  int y;
104  constexpr pixel(int);
105};
106
107constexpr pixel::pixel(int a)
108  : x(square(a)), y(square(a))
109  { }
110
111constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}}
112
113constexpr int square(int x) {
114  return x * x;
115}
116
117// FIXME: The initializer is a constant expression.
118constexpr pixel large(4); // unexpected-error {{must be initialized by a constant expression}}
119
120int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}}
121      return x + 1;
122}
123
124extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}}
125