[mlir][acc] Introduce MappableType interface (#122146)
[llvm-project.git] / clang / test / CXX / dcl.dcl / dcl.spec / dcl.constexpr / p1.cpp
blob752451f3d9749c598baae2277830dd8ea0cedb93
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
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
10 struct notlit { // expected-note {{not literal because}}
11 notlit() {}
13 struct notlit2 {
14 notlit2() {}
17 // valid declarations
18 constexpr int i1 = 0;
19 constexpr int f1() { return 0; }
20 struct s1 {
21 constexpr static int mi1 = 0;
22 const static int mi2;
24 constexpr int s1::mi2 = 0;
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
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 {{constexpr variable 'mi2' must be initialized by a constant expression}}
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}}
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
85 // template stuff
86 template <typename T> constexpr T ft(T t) { return 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 #if __cplusplus >= 201402L
93 // expected-note@-2 {{candidate template ignored: could not match 'T () const' against 'int ()'}}
94 #endif
97 // explicit specialization can differ in constepxr
98 template <> notlit ft(notlit nl) { return nl; }
99 template <> char ft(char c) { return c; } // expected-note {{previous}}
100 template <> constexpr char ft(char nl); // expected-error {{constexpr declaration of 'ft<char>' follows non-constexpr declaration}}
101 template <> constexpr int gt(int nl) { return nl; }
102 template <> notlit S::f() const { return notlit(); }
103 #if __cplusplus >= 201402L
104 // expected-error@-2 {{no function template matches}}
105 #endif
106 template <> constexpr int S::g() { return 0; }
107 #if __cplusplus < 201402L
108 // expected-warning@-2 {{C++14}}
109 // expected-note@-3 {{previous}}
110 #else
111 // expected-error@-5 {{no function template matches function template specialization 'g'}}
112 #endif
113 template <> int S::g() const;
114 #if __cplusplus < 201402L
115 // expected-error@-2 {{non-constexpr declaration of 'g<int>' follows constexpr declaration}}
116 #endif
117 // specializations can drop the 'constexpr' but not the implied 'const'.
118 template <> char S::g() { return 0; } // expected-error {{no function template matches}}
119 template <> double S::g() const { return 0; } // ok
121 constexpr int i3 = ft(1);
123 void test() {
124 // ignore constexpr when instantiating with non-literal
125 notlit2 nl2;
126 (void)ft(nl2);
129 // Examples from the standard:
130 constexpr int square(int x); // expected-note {{declared here}}
131 constexpr int bufsz = 1024;
133 constexpr struct pixel { // expected-error {{struct cannot be marked constexpr}}
134 int x;
135 int y;
136 constexpr pixel(int);
139 constexpr pixel::pixel(int a)
140 : x(square(a)), y(square(a)) // expected-note {{undefined function 'square' cannot be used in a constant expression}}
143 constexpr pixel small(2); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'pixel(2)'}}
145 constexpr int square(int x) {
146 return x * x;
149 constexpr pixel large(4);
151 int next(constexpr int x) { // expected-error {{function parameter cannot be constexpr}}
152 return x + 1;
155 extern constexpr int memsz; // expected-error {{constexpr variable declaration must be a definition}}
157 namespace {
158 struct A {
159 static constexpr int n = 0;
161 // FIXME: We should diagnose this prior to C++17.
162 const int &r = A::n;
165 #if __cplusplus < 201402L
166 namespace ImplicitConstexprDef {
167 struct A { // #defined-here
168 void f(); // expected-note {{member declaration does not match because it is not const qualified}}
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}}
175 #endif