Skip to content

Instantly share code, notes, and snippets.

@juliusHuelsmann
Created February 18, 2020 17:11
Show Gist options
  • Save juliusHuelsmann/c661aa556fa9dabb4c33a34bec4c3f76 to your computer and use it in GitHub Desktop.
Save juliusHuelsmann/c661aa556fa9dabb4c33a34bec4c3f76 to your computer and use it in GitHub Desktop.
Example implementation stripping the last few values of a tuple, expanded in conditional tuple example
// Author: Julius Hülsmann
// Email: juliusHuelsmann [at] gmail.com, julius.huelsmann [at] data-spree.com
// Creation Date: 2020-02-18
// Execution: g++-10.0 -std=c++2a conditional_tuuple_type.cc
// Example implementation stripping the last few values of a tuple, expanded in conditional tuple example
#include <tuple>
template <size_t s, typename is, typename ...args> struct thlp;
template <size_t s, size_t... i, class... args> struct thlp<s, std::index_sequence<i...>, args...> {
static_assert(s <= sizeof...(args), "Requested new tuple size exceeds old one.");
using type = std::tuple<std::tuple_element_t<i, std::tuple<args...>>...>;
};
template <size_t s, typename... args>
using conditional_tuple_t = thlp<s, decltype(std::make_index_sequence<s>()), args...>::type;
int main() {
struct exmpl { exmpl(){} virtual ~exmpl(){} }; //< example struct with virtual destructor
static_assert(std::is_same_v<std::tuple<exmpl, int>, conditional_tuple_t<2, exmpl, int, double>>);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment