| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
|
| 3 |
// Tests constrained decltype(auto). |
| 4 |
|
| 5 |
template<typename T> |
| 6 |
concept Type = true; |
| 7 |
|
| 8 |
template<typename T> |
| 9 |
concept Int = __is_same_as(T, int); |
| 10 |
|
| 11 |
template<typename T, typename U> |
| 12 |
concept SameAs = __is_same_as(T, U); |
| 13 |
|
| 14 |
template<typename T, typename U> |
| 15 |
requires SameAs<T, U> |
| 16 |
constexpr bool check = true; |
| 17 |
|
| 18 |
int z = 0; |
| 19 |
const int cz = 0; |
| 20 |
|
| 21 |
Type decltype(auto) x1 = 0; |
| 22 |
static_assert(check<decltype(x1), int>); |
| 23 |
Type decltype(auto) x2 = z; |
| 24 |
static_assert(check<decltype(x2), int>); |
| 25 |
Type decltype(auto) x3 = (z); |
| 26 |
static_assert(check<decltype(x3), int&>); |
| 27 |
Type decltype(auto) x4 = cz; |
| 28 |
static_assert(check<decltype(x4), const int>); |
| 29 |
Type decltype(auto) x5 = (cz); |
| 30 |
static_assert(check<decltype(x5), const int&>); |
| 31 |
|
| 32 |
Type decltype(auto) f1() { return 0; } |
| 33 |
static_assert(check<decltype(f1()), int>); |
| 34 |
Type decltype(auto) f2() { return z; } |
| 35 |
static_assert(check<decltype(f2()), int>); |
| 36 |
Type decltype(auto) f3() { return (z); } |
| 37 |
static_assert(check<decltype(f3()), int&>); |
| 38 |
Type decltype(auto) f4() { return cz; } |
| 39 |
static_assert(check<decltype(f4()), int>); // Top-level const is removed? |
| 40 |
Type decltype(auto) f5() { return (cz); } |
| 41 |
static_assert(check<decltype(f5()), const int&>); |
| 42 |
|
| 43 |
bool b = true; |
| 44 |
const bool cb = true; |
| 45 |
|
| 46 |
Int decltype(auto) b1 = true; // { dg-error "deduced initializer" } |
| 47 |
Int decltype(auto) b2 = (b); // { dg-error "deduced initializer" } |
| 48 |
Int decltype(auto) b3 = (cb); // { dg-error "deduced initializer" } |
| 49 |
|
| 50 |
Int decltype(auto) g1() { } // { dg-error "deduced return type" } |
| 51 |
Int decltype(auto) g2() { return; } // { dg-error "deduced return type" } |
| 52 |
Int decltype(auto) g3() { return true; } // { dg-error "deduced return type" } |
| 53 |
int g4(Type decltype(auto) x) { return 0; } // { dg-error "cannot declare" } |
| 54 |
int g5(decltype(auto) x) { return 0; } // { dg-error "cannot declare" } |
| 55 |
|
| 56 |
template<Type decltype(auto) X, typename T> |
| 57 |
requires SameAs<decltype(X), T> |
| 58 |
constexpr bool deduced_as = true; |
| 59 |
|
| 60 |
constexpr int Z = 10; |
| 61 |
|
| 62 |
static_assert(deduced_as<0, int>); |
| 63 |
static_assert(deduced_as<0, int&>); // { dg-error "invalid variable template" } |
| 64 |
static_assert(deduced_as<Z, int>); |
| 65 |
static_assert(deduced_as<(Z), const int>); // { dg-error "invalid variable template" } |
| 66 |
static_assert(deduced_as<(Z), const int&>); |
| 67 |
|