Last active
August 29, 2015 14:21
-
-
Save dgodfrey206/43771a0a7cbaad65631e to your computer and use it in GitHub Desktop.
C++14 Metafunction tools
This file contains hidden or 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 <tuple> | |
#include <cassert> | |
struct type_erasure { }; | |
template<class T> | |
struct wrapper : type_erasure { | |
wrapper(T&& w) : w_(std::forward<T>(w)) { } | |
T&& w_; | |
decltype(auto) get() { return std::forward<T>(w_); } | |
}; | |
template<class T> | |
wrapper<T> wrapper_for(T&& x) { | |
return { std::forward<T>(x) }; | |
} | |
template <typename ignore> | |
struct lookup; | |
template <std::size_t... ignore> | |
struct lookup<std::index_sequence<ignore...>> { | |
template <typename nth, typename... Ts> | |
static decltype(auto) | |
at_position(decltype(ignore, type_erasure())..., wrapper<nth> w, Ts&&...) { | |
return w.get(); | |
} | |
template<typename... Ts> | |
static auto | |
all_after(decltype(ignore, type_erasure())..., Ts&&... args) { | |
return std::forward_as_tuple(args.get()...); | |
} | |
}; | |
template<std::size_t index, typename... Args> | |
auto getNth(Args&&... args) { | |
return lookup<std::make_index_sequence<index>>::at_position( | |
wrapper_for(std::forward<Args>(args))... | |
); | |
} | |
template<std::size_t index, typename... Args> | |
auto getAllAfter(Args&&... args) { | |
return lookup<std::make_index_sequence<index + 1>>::all_after( | |
wrapper_for(std::forward<Args>(args))... | |
); | |
} | |
int main() | |
{ | |
assert(getNth<0>(1, 2, 3) == 1); | |
assert(getNth<1>(1, 2, 3) == 2); | |
assert(getNth<2>(1, 2, 3) == 3); | |
assert(getAllAfter<2>(2, 4, 6, 8, 10) == std::make_tuple(8, 10)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment