| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
// From N2235 |
| 4 |
|
| 5 |
// 4.1 constant-expression functions |
| 6 |
// 1 examples |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
// 2 defined before first use |
| 13 |
// NOTE: this is only needed in contexts that require a constant-expression |
| 14 |
struct S { |
| 15 |
constexpr int twice() const; |
| 16 |
constexpr int t() const; // { dg-message "used but never defined" } |
| 17 |
private: |
| 18 |
static constexpr int val = 7; // constexpr variable |
| 19 |
}; |
| 20 |
|
| 21 |
constexpr int S::twice() const { return val + val; } |
| 22 |
constexpr S s = { }; |
| 23 |
int x1 = s.twice(); // ok |
| 24 |
int x2 = s.t(); // error: S::t() not defined |
| 25 |
constexpr int x2a = s.t(); // { dg-error "S::t" } error: S::t() not defined |
| 26 |
constexpr int ff(); // ok |
| 27 |
constexpr int gg(); // ok |
| 28 |
int x3 = ff(); // error: ff() not defined |
| 29 |
constexpr int x3a = ff(); // { dg-error "ff" } error: ff() not defined |
| 30 |
constexpr int ff() { return 1; } // too late |
| 31 |
constexpr int gg() { return 2; } |
| 32 |
int x4 = gg(); // ok |
| 33 |
|
| 34 |
|
| 35 |
// 4.2 const-expression data |
| 36 |
|
| 37 |
// 2 |
| 38 |
// storage not allocated untill address taken |
| 39 |
constexpr double x = 9484.748; |
| 40 |
const double* p = &x; // the &x forces x into memory |
| 41 |
|
| 42 |
// 4.3 constant-expression constructors |
| 43 |
|
| 44 |
// 1 |
| 45 |
struct complex { |
| 46 |
constexpr complex(double r, double i) : re(r), im(i) { } |
| 47 |
constexpr double real() const { return re; } |
| 48 |
constexpr double imag() const { return im; } |
| 49 |
private: |
| 50 |
double re; |
| 51 |
double im; |
| 52 |
}; |
| 53 |
constexpr complex I(0, 1); // OK -- literal complex |
| 54 |
|
| 55 |
|
| 56 |
// 2 invoked with non-const args |
| 57 |
double x5 = 1.0; // { dg-message "not declared .constexpr." } |
| 58 |
constexpr complex unit(x5, 0); // { dg-error "x5|argument" } error: x5 non-constant |
| 59 |
const complex one(x5, 0); // OK, ‘‘ordinary const’’ -- dynamic |
| 60 |
// initialization |
| 61 |
constexpr double xx = I.real(); // OK |
| 62 |
complex z(2, 4); // OK -- ordinary variable |
| 63 |
|
| 64 |
// 3 |
| 65 |
constexpr complex v[] = { |
| 66 |
complex(0, 0), complex(1, 1), complex(2, 2) |
| 67 |
}; |
| 68 |
constexpr double x6 = v[2].real(); // OK |
| 69 |
|
| 70 |
// 4 |
| 71 |
constexpr int i = 98; |
| 72 |
typedef __INTPTR_TYPE__ intptr_t; |
| 73 |
constexpr intptr_t ip = (intptr_t) &i; // { dg-error "constant" } |
| 74 |
|
| 75 |
// 4.3.2 copy-constructor |
| 76 |
constexpr complex operator+(complex z, complex w) |
| 77 |
{ |
| 78 |
return complex(z.real() + w.real(), z.imag() + w.imag()); // fine |
| 79 |
} |
| 80 |
constexpr complex I2 = I + I; // OK |
| 81 |
struct resource { |
| 82 |
int id; |
| 83 |
constexpr resource(int i) : id(i) { } // fine |
| 84 |
resource(const resource& r) : id(r.id) // oops, not constexpr |
| 85 |
{ |
| 86 |
//cout << id << " copied" << endl; |
| 87 |
} |
| 88 |
}; |
| 89 |
constexpr resource f(resource d) |
| 90 |
{ return d; } // { dg-error "non-.constexpr." "" { target { { { ! implicit_constexpr } && c++20_down } || c++11_only } } } |
| 91 |
// { dg-error "non-.constexpr." "" { target { c++23 && { ! implicit_constexpr } } } .-1 } |
| 92 |
constexpr resource d = f(9); // { dg-message ".constexpr." "" { target { { ! implicit_constexpr } || c++11_only } } } |
| 93 |
|
| 94 |
// 4.4 floating-point constant expressions |