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