| 1 |
// P2280R4 - Using unknown pointers and references in constant expressions |
| 2 |
// PR c++/106650 |
| 3 |
// { dg-do compile { target c++11 } } |
| 4 |
|
| 5 |
using size_t = decltype(sizeof(42)); |
| 6 |
|
| 7 |
template <typename T, size_t N> |
| 8 |
constexpr auto array_size(T (&)[N]) -> size_t { |
| 9 |
return N; |
| 10 |
} |
| 11 |
|
| 12 |
extern int (&r)[42]; |
| 13 |
constexpr int i = array_size (r); |
| 14 |
|
| 15 |
void check(int const (¶m)[3]) { |
| 16 |
int local[] = {1, 2, 3}; |
| 17 |
constexpr auto s0 = array_size(local); |
| 18 |
constexpr auto s1 = array_size(param); |
| 19 |
} |
| 20 |
|
| 21 |
template <typename T, size_t N> |
| 22 |
constexpr size_t array_size_ptr(T (*)[N]) { |
| 23 |
return N; |
| 24 |
} |
| 25 |
|
| 26 |
void check_ptr(int const (*param)[3]) { |
| 27 |
constexpr auto s2 = array_size_ptr(param); // { dg-error "not a constant" } |
| 28 |
} |
| 29 |
|
| 30 |
struct A |
| 31 |
{ |
| 32 |
constexpr int f() { return 42; } |
| 33 |
void g() { constexpr int i = f(); } |
| 34 |
void g2() { constexpr int i = this->f(); } |
| 35 |
}; |
| 36 |
|
| 37 |
struct B { |
| 38 |
constexpr static bool b = false; |
| 39 |
void g() noexcept(b) { } |
| 40 |
void g2() noexcept(this->b) { } |
| 41 |
}; |