variadic: rework get to infer references in return types

This commit is contained in:
Danny Robson 2018-07-04 14:04:56 +10:00
parent 6ee256992d
commit b3c918888a

View File

@ -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: /// Returns the argument at index `IndexV', as if we called:
/// std::get<N> (std::make_tuple (...)) /// std::get<N> (std::make_tuple (...))
template <std::size_t IndexV, typename HeadT, typename ...TailT> template <size_t IndexV, typename ...ArgsT>
auto decltype(auto)
get (HeadT &&head, TailT &&...tail) noexcept ((std::is_nothrow_move_constructible_v<TailT> && ...)) get (ArgsT&& ...args)
{ {
static_assert (IndexV < sizeof... (TailT) + 1, "Index is out of bounds"); return detail::getter<IndexV> {}(std::forward<ArgsT> (args)...);
if constexpr (IndexV == 0)
return std::forward<HeadT> (head);
else
return get<IndexV-1> (std::forward<TailT> (tail)...);
} }