From 98afc8feaed67a0c6d450d27ded25145476ac8ff Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Feb 2025 02:33:20 +0000 Subject: Add utility class to easily get nth field of tuple/pair for any types --- lib/util.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib') diff --git a/lib/util.h b/lib/util.h index 290492f..2674eaf 100644 --- a/lib/util.h +++ b/lib/util.h @@ -3,6 +3,7 @@ #include // IWYU pragma: keep #include #include +#include template constexpr auto @@ -12,3 +13,23 @@ transform_array(const std::array & in, auto && transform) std::transform(in.begin(), in.end(), out.begin(), transform); return out; } + +namespace { + template struct GetNth { + decltype(auto) + operator()(const auto & tup) const + { + if constexpr (sizeof...(N) == 1) { + return std::get(tup); + } + else { + return std::tie(std::get(tup)...); + } + } + }; +} + +template constexpr auto Nth = GetNth {}; +constexpr auto GetFirst = Nth<0>; +constexpr auto GetSecond = Nth<1>; +constexpr auto GetSwapped = Nth<0, 1>; -- cgit v1.2.3