Skip to content

Commit

Permalink
🥅 Better error messages when tuple indexing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeane-intel committed Jul 11, 2023
1 parent 0a4cfab commit f27f460
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/cib/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ struct index_pair {
std::size_t inner;
};

template <typename...> constexpr auto always_false_v = false;

template <std::size_t... Is, template <typename> typename... Fs, typename... Ts>
struct tuple_impl<std::index_sequence<Is...>, index_function_list<Fs...>, Ts...>
: element_helper<Fs...>::template element_t<Is, Ts>... {
Expand Down Expand Up @@ -269,19 +271,35 @@ struct tuple_impl<std::index_sequence<Is...>, index_function_list<Fs...>, Ts...>
template <std::size_t I>
[[nodiscard]] constexpr auto
operator[](index_constant<I> *i) const & -> decltype(auto) {
static_assert(I < sizeof...(Ts), "Tuple index out of bounds!");
return this->ugly_iGet_clvr(i);
}
template <std::size_t I>
[[nodiscard]] constexpr auto
operator[](index_constant<I> *i) & -> decltype(auto) {
static_assert(I < sizeof...(Ts), "Tuple index out of bounds!");
return this->ugly_iGet_lvr(i);
}
template <std::size_t I>
[[nodiscard]] constexpr auto
operator[](index_constant<I> *i) && -> decltype(auto) {
static_assert(I < sizeof...(Ts), "Tuple index out of bounds!");
return std::move(*this).ugly_iGet_rvr(i);
}

constexpr auto ugly_tGet_clvr(auto idx) const & -> void {
static_assert(always_false_v<decltype(idx), Ts...>,
"Type not found in tuple!");
}
constexpr auto ugly_tGet_lvr(auto idx) & -> void {
static_assert(always_false_v<decltype(idx), Ts...>,
"Type not found in tuple!");
}
constexpr auto ugly_tGet_rvr(auto idx) && -> void {
static_assert(always_false_v<decltype(idx), Ts...>,
"Type not found in tuple!");
}

[[nodiscard]] constexpr auto get(auto idx) const & -> decltype(auto) {
return this->ugly_tGet_clvr(idx);
}
Expand Down

0 comments on commit f27f460

Please sign in to comment.