Created
September 19, 2019 09:26
-
-
Save sigman78/ea50f0ee70299ca5ab3d3ea85870c45f to your computer and use it in GitHub Desktop.
easy tuple on variadics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
#include <cstdio> | |
template <typename ... Ts> | |
constexpr auto tuple(Ts&& ... ts) | |
{ | |
return [...ts = std::forward<Ts>(ts)](auto&& f) { return f(ts...);}; | |
} | |
template <unsigned I, typename T, typename ... Ts> | |
constexpr auto get_val(const T& t, const Ts& ... ts) | |
{ | |
if constexpr (I == 0) return t; | |
else return get_val<I-1>(ts...); | |
} | |
template <unsigned I, typename T> | |
constexpr decltype(auto) get(T&& t) | |
{ | |
return t([](auto && ... vs){ return get_val<I>(std::forward<decltype(vs)>(vs)...);}); | |
} | |
template <typename T> | |
constexpr auto size(T&& t) | |
{ | |
return t([](const auto& ... ts){ return sizeof...(ts);}); | |
} | |
int main() | |
{ | |
constexpr auto t = tuple(1, "foo", 3.14); | |
constexpr auto n = size(t); | |
std::puts(get<1>(t)); | |
return get<n-1>(t) - get<0>(t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment