summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-02-14 02:33:20 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2025-02-14 02:33:20 +0000
commit98afc8feaed67a0c6d450d27ded25145476ac8ff (patch)
tree70f962ab5243efb906cd42a1a31d7c8cdbfef07b /lib
parentAdd operator| overload to make OpenMesh XY_range(...) work with std::ranges (diff)
downloadilt-98afc8feaed67a0c6d450d27ded25145476ac8ff.tar.bz2
ilt-98afc8feaed67a0c6d450d27ded25145476ac8ff.tar.xz
ilt-98afc8feaed67a0c6d450d27ded25145476ac8ff.zip
Add utility class to easily get nth field of tuple/pair for any types
Diffstat (limited to 'lib')
-rw-r--r--lib/util.h21
1 files changed, 21 insertions, 0 deletions
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 <algorithm> // IWYU pragma: keep
#include <array>
#include <cstddef>
+#include <tuple>
template<typename T, std::size_t N>
constexpr auto
@@ -12,3 +13,23 @@ transform_array(const std::array<T, N> & in, auto && transform)
std::transform(in.begin(), in.end(), out.begin(), transform);
return out;
}
+
+namespace {
+ template<size_t... N> struct GetNth {
+ decltype(auto)
+ operator()(const auto & tup) const
+ {
+ if constexpr (sizeof...(N) == 1) {
+ return std::get<N...>(tup);
+ }
+ else {
+ return std::tie(std::get<N>(tup)...);
+ }
+ }
+ };
+}
+
+template<size_t... N> constexpr auto Nth = GetNth<N...> {};
+constexpr auto GetFirst = Nth<0>;
+constexpr auto GetSecond = Nth<1>;
+constexpr auto GetSwapped = Nth<0, 1>;