1 // Negative examples from N3092 (FCD)
2 // { dg-do compile { target c++11 } }
3
4 // OK: declaration
5 constexpr int square(int x); // { dg-message "never defined" }
6
7 // error: pixel is a type
8 constexpr struct pixel { // { dg-error ".constexpr." }
9 int x;
10 int y;
11 // OK: declaration
12 constexpr pixel(int);
13 };
pixel(int a)14 constexpr pixel::pixel(int a)
15 // OK: definition
16 : x(square(a)), y(square(a)) // { dg-error "square" }
17 { }
18
19 // error: square not defined, so small(2) not constant (5.19), so constexpr
20 // not satisfied
21 constexpr pixel small(2); // { dg-message "in .constexpr. expansion of " }
22
23 // error: not for parameters
next(constexpr int x)24 int next(constexpr int x) { // { dg-error "parameter" }
25 return x + 1;
26 }
27
28 // error: not a definition
29 extern constexpr int memsz; // { dg-error "definition" }
30
31 // error: return type is void
f(int x)32 constexpr void f(int x) // { dg-error "void" "" { target c++11_only } }
33 { /* ... */ }
34 // error: use of decrement
prev(int x)35 constexpr int prev(int x)
36 { return --x; } // { dg-error "-- x" "" { target c++11_only } }
37
38 // error: body not just return expr
g(int x,int n)39 constexpr int g(int x, int n) {
40 int r = 1;
41 while (--n > 0) r *= x;
42 return r;
43 } // { dg-error "body of .constexpr. function" "" { target c++11_only } }
44
45 class debug_flag {
46 public:
47 explicit debug_flag(bool);
48 constexpr bool is_on(); // { dg-error "not a literal type" "" { target c++11_only } } debug_flag not literal type
49 private:
50 bool flag;
51 };
52 // OK
bar(int x,int y)53 constexpr int bar(int x, int y) // { dg-message "previously defined here" }
54 { return x + y + x*y; }
55 // ...
56 // error: redefinition of bar
bar(int x,int y)57 int bar(int x, int y) // { dg-error "redefinition" }
58 { return x * 2 + 3 * y; }
59
60 struct pixel2 { // { dg-message "no user-provided default constructor" }
61 int x, y;
62 };
63 constexpr pixel2 ur = { 1294, 1024 };// OK
64 constexpr pixel2 origin; // { dg-error "uninitialized 'const" }
65
addr(const int & ir)66 constexpr const int* addr(const int& ir) { return &ir; } // OK
67
68 // error, initializer for constexpr variable not a constant
69 extern constexpr const int* tp = addr(5); // { dg-error "" }
70