variadic: rework get
to infer references in return types
This commit is contained in:
parent
6ee256992d
commit
b3c918888a
38
variadic.hpp
38
variadic.hpp
@ -41,18 +41,38 @@ namespace util::variadic {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
namespace detail {
|
||||
// using the strategy of discarding assignment from:
|
||||
// https://attugit.github.io/2015/02/Accessing-nth-element-of-parameter-pack/
|
||||
struct discard {
|
||||
template <typename ArgsT>
|
||||
constexpr discard (ArgsT&&...) { ; }
|
||||
};
|
||||
|
||||
template <size_t>
|
||||
using discard_n = discard;
|
||||
|
||||
template <std::size_t N, typename = std::make_index_sequence<N>>
|
||||
struct getter;
|
||||
|
||||
template <std::size_t N, std::size_t... ignore>
|
||||
struct getter<N, std::index_sequence<ignore...>> {
|
||||
template <typename ValueT, typename ...TailT>
|
||||
decltype(auto)
|
||||
operator() (discard_n<ignore>..., ValueT &&val, TailT&&...) const
|
||||
{
|
||||
return std::forward<ValueT> (val);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the argument at index `IndexV', as if we called:
|
||||
/// std::get<N> (std::make_tuple (...))
|
||||
template <std::size_t IndexV, typename HeadT, typename ...TailT>
|
||||
auto
|
||||
get (HeadT &&head, TailT &&...tail) noexcept ((std::is_nothrow_move_constructible_v<TailT> && ...))
|
||||
template <size_t IndexV, typename ...ArgsT>
|
||||
decltype(auto)
|
||||
get (ArgsT&& ...args)
|
||||
{
|
||||
static_assert (IndexV < sizeof... (TailT) + 1, "Index is out of bounds");
|
||||
|
||||
if constexpr (IndexV == 0)
|
||||
return std::forward<HeadT> (head);
|
||||
else
|
||||
return get<IndexV-1> (std::forward<TailT> (tail)...);
|
||||
return detail::getter<IndexV> {}(std::forward<ArgsT> (args)...);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user