1 // { dg-do compile }
2 // { dg-options "-std=c++2a" }
3
SS4 struct S { S () : a (0), b (1) {} int a, b; };
5 int f1 (); // { dg-message "previous declaration 'int f1\\(\\)'" }
6 consteval int f1 (); // { dg-error "redeclaration 'consteval int f1\\(\\)' differs in 'consteval' from previous declaration" }
7 consteval int f2 (); // { dg-message "previous declaration 'consteval int f2\\(\\)'" }
8 int f2 (); // { dg-error "redeclaration 'int f2\\(\\)' differs in 'consteval' from previous declaration" }
9 constexpr int f3 (); // { dg-message "previous declaration 'constexpr int f3\\(\\)'" }
10 consteval int f3 (); // { dg-error "redeclaration 'consteval int f3\\(\\)' differs in 'consteval' from previous declaration" }
11 consteval int f4 (); // { dg-message "previous declaration 'consteval int f4\\(\\)'" }
12 constexpr int f4 (); // { dg-error "redeclaration 'constexpr int f4\\(\\)' differs in 'consteval' from previous declaration" }
13 typedef consteval int cint; // { dg-error "'consteval' cannot appear in a typedef declaration" }
14 consteval struct T { int i; }; // { dg-error "'consteval' cannot be used for type declarations" }
15 consteval int a = 5; // { dg-error "a variable cannot be declared 'consteval'" }
16 consteval auto [ b, c ] = S (); // { dg-error "structured binding declaration cannot be 'consteval'" }
f5(consteval int x)17 int f5 (consteval int x) { return x; } // { dg-error "a parameter cannot be declared 'consteval'" }
f6(int x)18 consteval int f6 (int x) { return x; }
19 int d = 6; // { dg-message "'int d' is not const" }
20 int e = f6 (d); // { dg-error "the value of 'd' is not usable in a constant expression" }
f7(int x)21 constexpr int f7 (int x) { return f6 (x); } // { dg-error "'x' is not a constant expression" }
22 constexpr int f = f7 (5); // { dg-error "" }
23 // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 }
24 using fnptr = int (int);
25 fnptr *g = f6; // { dg-error "taking address of an immediate function 'consteval int f6\\(int\\)'" }
26 int f8 (fnptr *);
27 int h = f8 (f6); // { dg-error "taking address of an immediate function 'consteval int f6\\(int\\)'" }
f9()28 consteval constexpr int f9 () { return 0; } // { dg-error "both 'constexpr' and 'consteval' specified" }
f10()29 constexpr consteval int f10 () { return 0; } // { dg-error "both 'constexpr' and 'consteval' specified" }
f11()30 consteval consteval int f11 () { return 0; } // { dg-error "duplicate 'consteval'" }
~UU31 struct U { consteval ~U () {} }; // { dg-error "a destructor cannot be 'consteval'" }
32 struct V { consteval int v = 5; }; // { dg-error "non-static data member 'v' declared 'consteval'" }
33 struct W { consteval static int w; }; // { dg-error "static data member 'w' declared 'consteval'" }
34 int i = sizeof (&f6); // { dg-bogus "taking address of an immediate function 'consteval int f6\\(int\\)'" }
35 using j = decltype (&f6); // { dg-bogus "taking address of an immediate function 'consteval int f6\\(int\\)'" }
36 int k = sizeof (f6 (d)); // { dg-bogus "the value of 'd' is not usable in a constant expression" }
37 using l = decltype (f6 (d)); // { dg-bogus "the value of 'd' is not usable in a constant expression" }
38 bool m = noexcept (f6 (d)); // { dg-bogus "the value of 'd' is not usable in a constant expression" }
39 namespace std {
40 using size_t = decltype (sizeof (0));
41 }
42 consteval void* operator new (std::size_t); // { dg-error "'operator new' cannot be 'consteval'" }
43 consteval void operator delete (void *, std::size_t) noexcept; // { dg-error "'operator delete' cannot be 'consteval'" }
44 consteval void operator delete[] (void *) noexcept; // { dg-error "'operator delete \\\[\\\]' cannot be 'consteval'" }
45 struct X {
46 static consteval void* operator new (std::size_t); // { dg-error "'operator new' cannot be 'consteval'" }
47 static consteval void operator delete (void *, std::size_t) noexcept; // { dg-error "'operator delete' cannot be 'consteval'" }
48 consteval static void operator delete[] (void *) noexcept; // { dg-error "'operator delete \\\[\\\]' cannot be 'consteval'" }
49 };
main()50 consteval int main () { return 0; } // { dg-error "cannot declare '::main' to be 'consteval'" }
51 struct A { A (); int a; }; // { dg-message "defaulted constructor calls non-'constexpr' 'A::A\\(\\)'" }
BB52 struct B { constexpr B () : b (0) {} int b; };
53 struct C { A a; consteval C () = default; }; // { dg-error "explicitly defaulted function 'consteval C::C\\(\\)' cannot be declared 'consteval' because the implicit declaration is not 'constexpr'" }
54 struct D { B b; consteval D () = default; };
f12(T x)55 template <class T> consteval T f12 (T x) { return x; }
56 template consteval float f12 (float x); // { dg-error "explicit instantiation shall not use 'consteval' specifier" }
57 consteval int
f13(int x)58 f13 (int x)
59 {
60 static int a = 5; // { dg-error "'a' declared 'static' in 'consteval' function" }
61 thread_local int b = 6; // { dg-error "'b' declared 'thread_local' in 'consteval' function" }
62 return x;
63 }
64