2018-11-07 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / g++.dg / cpp0x / constexpr-neg1.C
blob336699292a2eb4193b71c14c21725d7bc0bf0bfa
1 // Negative examples from N3092 (FCD)
2 // { dg-do compile { target c++11 } }
4 // OK: declaration
5 constexpr int square(int x);    // { dg-message "never defined" }
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);
14 constexpr pixel::pixel(int a)
15 // OK: definition
16   : x(square(a)), y(square(a))  // { dg-error "square" }
17 { }
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 " }
23 // error: not for parameters
24 int next(constexpr int x) {     // { dg-error "parameter" }
25   return x + 1;
28 // error: not a definition
29 extern constexpr int memsz;     // { dg-error "definition" }
31 // error: return type is void
32 constexpr void f(int x)         // { dg-error "void" "" { target c++11_only } }
33 { /* ... */ }
34 // error: use of decrement
35 constexpr int prev(int x)
36 { return --x; }                 // { dg-error "-- x" "" { target c++11_only } }
38 // error: body not just return expr
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 } }
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;
52 // OK
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
57 int bar(int x, int y)           // { dg-error "redefinition" }
58 { return x * 2 + 3 * y; }
60 struct pixel2 {    // { dg-message "no user-provided default constructor" }
61   int x, y;
63 constexpr pixel2 ur = { 1294, 1024 };// OK
64 constexpr pixel2 origin;             // { dg-error "uninitialized const" }
66 constexpr const int* addr(const int& ir) { return &ir; } // OK
68 // error, initializer for constexpr variable not a constant
69 extern constexpr const int* tp = addr(5); // { dg-error "" }