Skip to content

Instantly share code, notes, and snippets.

@simonrenger
Last active December 27, 2018 14:33
Show Gist options
  • Save simonrenger/8dfa525ae87397ca87a5228d3def65a7 to your computer and use it in GitHub Desktop.
Save simonrenger/8dfa525ae87397ca87a5228d3def65a7 to your computer and use it in GitHub Desktop.
C++ Tuple to type generation
#include <tuple>
///////////////////////////////////////////////// TUPLE GENERATOR /////////////////////////////////////////////////
template <unsigned int N, typename T>
struct TupleGenerator {
using type = decltype(std::tuple_cat(std::tuple<T>(), typename TupleGenerator<N - 1, T>::type()));
};
template <typename T>
struct TupleGenerator<0, T> {
using type = decltype(std::tuple<>());
};
template <unsigned int N, typename T>
using TupleGenerator_t = typename TupleGenerator<N, T>::type;
///////////////////////////////////////////////// TUPLE TO PACK /////////////////////////////////////////////////
template<template<typename...> class T, typename>
struct CreateTypeWithTuple { };
template<template<typename...> class T, typename... Ts>
struct CreateTypeWithTuple<T, std::tuple<Ts...>>
{
using type = T<Ts...>;
};
template<template<typename...> class T, typename... Ts>
using CreateTypeWithTuple_t = typename CreateTypeWithTuple <T,Ts...>::type;
///////////////////////////////////////////////// EXAMPLE /////////////////////////////////////////////////
template<typename... Ts>
struct container { };
auto main() ->int
{
using X = container<float,float,float>;
using Y = CreateTypeWithTuple_t<container, TupleGenerator_t<3, float>>;
static_assert(
std::is_same<Y,X>::value,
"Error!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment