1 // { dg-do compile { target c++11 } }
5 // 4.1 constant-expression functions
12 // 2 defined before first use
13 // NOTE: this is only needed in contexts that require a constant-expression
15 constexpr int twice() const;
16 constexpr int t() const; // { dg-message "used but never defined" }
18 static constexpr int val = 7; // constexpr variable
21 constexpr int S::twice() const { return val + val; }
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; }
35 // 4.2 const-expression data
38 // storage not allocated untill address taken
39 constexpr double x = 9484.748;
40 const double* p = &x; // the &x forces x into memory
42 // 4.3 constant-expression constructors
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; }
53 constexpr complex I(0, 1); // OK -- literal complex
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
61 constexpr double xx = I.real(); // OK
62 complex z(2, 4); // OK -- ordinary variable
65 constexpr complex v[] = {
66 complex(0, 0), complex(1, 1), complex(2, 2)
68 constexpr double x6 = v[2].real(); // OK
72 typedef __INTPTR_TYPE__ intptr_t;
73 constexpr intptr_t ip = (intptr_t) &i; // { dg-error "constant" }
75 // 4.3.2 copy-constructor
76 constexpr complex operator+(complex z, complex w)
78 return complex(z.real() + w.real(), z.imag() + w.imag()); // fine
80 constexpr complex I2 = I + I; // OK
83 constexpr resource(int i) : id(i) { } // fine
84 resource(const resource& r) : id(r.id) // oops, not constexpr
86 //cout << id << " copied" << endl;
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 } } }
94 // 4.4 floating-point constant expressions