| 1 |
// PR c++/114138 |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
|
| 4 |
namespace std { |
| 5 |
template <class T> |
| 6 |
T&& declval() noexcept requires true; |
| 7 |
|
| 8 |
template <class> |
| 9 |
void declval() noexcept; |
| 10 |
|
| 11 |
namespace detail { |
| 12 |
struct none_such; |
| 13 |
template <class> |
| 14 |
using none_such_t = none_such; |
| 15 |
|
| 16 |
template <class T> |
| 17 |
extern const none_such_t<T> _getter_for; |
| 18 |
|
| 19 |
template <class T> |
| 20 |
using _decay_t = decltype(auto(declval<T>())); |
| 21 |
|
| 22 |
static_assert(__is_same_as(_decay_t<void>, void)); |
| 23 |
} |
| 24 |
|
| 25 |
template <const auto& Fn, class... Args> |
| 26 |
using _result_of_t = decltype(Fn(declval<Args>()...)); |
| 27 |
|
| 28 |
template <unsigned I, class Tuple> |
| 29 |
using tuple_element_t = _result_of_t<detail::_getter_for<detail::_decay_t<Tuple>>, char(*)[I+1], Tuple>; |
| 30 |
|
| 31 |
template <class First, class Second> |
| 32 |
struct pair { |
| 33 |
First first; |
| 34 |
Second second; |
| 35 |
}; |
| 36 |
|
| 37 |
template <class> |
| 38 |
inline constexpr bool _is_pair = false; |
| 39 |
|
| 40 |
template <class First, class Second> |
| 41 |
inline constexpr bool _is_pair<pair<First, Second>> = true; |
| 42 |
|
| 43 |
template <class T> |
| 44 |
concept Pair = _is_pair<decltype(auto(std::declval<T>()))>; |
| 45 |
|
| 46 |
template <unsigned I, Pair P> |
| 47 |
requires (I <= 1) |
| 48 |
decltype(auto) get(P&& p) noexcept { |
| 49 |
if constexpr (I == 0) { |
| 50 |
return (static_cast<P&&>(p).first); |
| 51 |
} else { |
| 52 |
return (static_cast<P&&>(p).second); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
namespace detail { |
| 57 |
inline constexpr auto _pair_getter = |
| 58 |
[]<unsigned J, class Pair>(char(*)[J], Pair&& p) noexcept -> decltype(auto) { |
| 59 |
return std::get<J-1>(static_cast<Pair&&>(p)); |
| 60 |
}; |
| 61 |
|
| 62 |
template <class First, class Second> |
| 63 |
inline constexpr auto _getter_for<pair<First, Second>> = _pair_getter; |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
static_assert(__is_same_as(int&, std::tuple_element_t<0, std::pair<int, float>&>)); |
| 69 |
static_assert(__is_same_as(float&&, std::tuple_element_t<1, std::pair<int, float>&&>)); |