File: constexpr-ex1.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (94 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// { dg-do compile { target c++11 } }

// From N2235

// 4.1 constant-expression functions
// 1 examples





// 2 defined before first use
// NOTE: this is only needed in contexts that require a constant-expression
struct S {
    constexpr int twice() const;
    constexpr int t() const;	// { dg-message "used but never defined" }
private:
    static constexpr int val = 7;  // constexpr variable
};

constexpr int S::twice() const { return val + val; }
constexpr S s = { };
int x1 = s.twice();     // ok
int x2 = s.t();         // error: S::t() not defined
constexpr int x2a = s.t();     // { dg-error "S::t" } error: S::t() not defined
constexpr int ff();     // ok
constexpr int gg();     // ok
int x3 = ff();          // error: ff() not defined
constexpr int x3a = ff();      // { dg-error "ff" } error: ff() not defined
constexpr int ff() { return 1; }        // too late
constexpr int gg() { return 2; }
int x4 = gg();  // ok


// 4.2 const-expression data

// 2
// storage  not allocated untill address taken
constexpr double x = 9484.748;
const double* p = &x;          // the &x forces x into memory

// 4.3 constant-expression constructors

// 1
struct complex {
   constexpr complex(double r, double i) : re(r), im(i) { }
   constexpr double real() const { return re; }
   constexpr double imag() const { return im; }
private:
   double re;
   double im;
};
constexpr complex I(0, 1);  // OK -- literal complex


// 2 invoked with non-const args
double x5 = 1.0;	       // { dg-message "not declared .constexpr." }
constexpr complex unit(x5, 0);	// { dg-error "x5|argument" } error: x5 non-constant
const complex one(x5, 0);   // OK, ‘‘ordinary const’’ -- dynamic
                           //   initialization
constexpr double xx = I.real(); // OK
complex z(2, 4);           // OK -- ordinary variable

// 3
constexpr complex v[] = {
     complex(0, 0), complex(1, 1), complex(2, 2)
};
constexpr double x6 = v[2].real(); // OK

// 4 
  constexpr int i = 98;
  typedef __INTPTR_TYPE__ intptr_t;
  constexpr intptr_t ip = (intptr_t) &i;	// { dg-error "constant" }

// 4.3.2 copy-constructor
constexpr complex operator+(complex z, complex w)
{
  return complex(z.real() + w.real(), z.imag() + w.imag()); // fine
}
constexpr complex I2 = I + I;                 // OK
struct resource {
  int id;
  constexpr resource(int i) : id(i) { }       // fine
  resource(const resource& r) : id(r.id)      // oops, not constexpr
  {
    //cout << id << " copied" << endl;
  }
};
constexpr resource f(resource d)
{ return d; }                  // { dg-error "non-.constexpr." "" { target { { { ! implicit_constexpr } && c++20_down } || c++11_only } } }
// { dg-error "non-.constexpr." "" { target { c++23 && { ! implicit_constexpr } } } .-1 }
constexpr resource d = f(9);   // { dg-message ".constexpr." "" { target { { ! implicit_constexpr } || c++11_only } } }

// 4.4 floating-point constant expressions