Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constexpr-ref1.C

raw
1 // P2280R4 - Using unknown pointers and references in constant expressions
2 // PR c++/106650
3 // { dg-do compile { target c++20 } }
4
5 #include <typeinfo>
6
7 using size_t = decltype(sizeof(42));
8
9 template <typename T, size_t N>
10 constexpr size_t array_size(T (&)[N]) {
11 return N;
12 }
13
14 void use_array(int const (&gold_medal_mel)[2]) {
15 constexpr auto gold = array_size(gold_medal_mel); // OK
16 }
17
18 constexpr auto olympic_mile() {
19 const int ledecky = 1500;
20 return []{ return ledecky; };
21 }
22 static_assert(olympic_mile()() == 1500); // OK
23
24 struct Swim {
25 constexpr int phelps() { return 28; }
26 virtual constexpr int lochte() { return 12; }
27 int coughlin = 12;
28 };
29
30 constexpr int how_many(Swim& swam) {
31 Swim* p = &swam;
32 return (p + 1 - 1)->phelps();
33 }
34
35 void splash(Swim& swam) {
36 static_assert(swam.phelps() == 28); // OK
37 static_assert((&swam)->phelps() == 28); // OK
38
39 Swim* pswam = &swam;
40 static_assert(pswam->phelps() == 28); // { dg-error "non-constant|not usable" }
41
42 static_assert(how_many(swam) == 28); // OK
43 static_assert(Swim().lochte() == 12); // OK
44
45 static_assert(swam.lochte() == 12); // { dg-error "non-constant|not a constant" }
46
47 static_assert(swam.coughlin == 12); // { dg-error "non-constant|not a constant" }
48 }
49
50 extern Swim dc;
51 extern Swim& trident;
52
53 constexpr auto& sandeno = typeid(dc); // OK, can only be typeid(Swim)
54 constexpr auto& gallagher = typeid(trident); // { dg-error "not a constant" }