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/util.h') 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 From 619983e949fde226cc7a3de0bc198fdcee3fe3b4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Feb 2025 14:46:48 +0000 Subject: Fixes and tests to new range helpers --- lib/util.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/util.h') diff --git a/lib/util.h b/lib/util.h index 2674eaf..cd7971b 100644 --- a/lib/util.h +++ b/lib/util.h @@ -29,7 +29,7 @@ namespace { }; } -template constexpr auto Nth = GetNth {}; -constexpr auto GetFirst = Nth<0>; -constexpr auto GetSecond = Nth<1>; -constexpr auto GetSwapped = Nth<0, 1>; +template inline constexpr auto Nth = GetNth {}; +inline constexpr auto GetFirst = Nth<0>; +inline constexpr auto GetSecond = Nth<1>; +inline constexpr auto GetSwapped = Nth<0, 1>; -- cgit v1.2.3