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 <type_traits> | |
template <typename...TArgs> | |
struct TypeList {}; | |
// Assume TElement is not in the list unless proven otherwise | |
template < typename TElement, typename TList > | |
struct ContainsType { | |
static constexpr bool value = false; | |
}; | |
// If it matches the first type, it is definitely in the list | |
template < typename TElement, typename... TTail > | |
struct ContainsType < TElement, TypeList < TElement, TTail... > > | |
{ | |
static constexpr bool value = true; | |
}; | |
// If it is not the first element, check the remaining list | |
template < typename TElement, typename THead, typename... TTail > | |
struct ContainsType < TElement, TypeList < THead, TTail... > > | |
{ | |
static constexpr bool value = (ContainsType < TElement, TypeList < TTail... > >::value); | |
}; | |
template <typename ...Types> | |
struct TypeAdder; | |
template<class ...Types> | |
struct BuilderType {}; | |
template<typename ...Types> | |
struct BuilderType<TypeList<Types...> > { | |
typedef TypeAdder<Types...> TypeAdder; | |
}; | |
template<typename T, typename TList> | |
struct AddType; | |
template<typename T, typename ... TArgs> | |
struct AddType< T, TypeList<TArgs...> > | |
{ | |
typedef typename std::conditional< | |
ContainsType<T, TypeList<TArgs...> >::value, | |
TypeList<TArgs... >, | |
TypeList<T, TArgs... > | |
>::type type; | |
}; | |
template <typename ...Types> | |
struct TypeAdder | |
{ | |
template <typename T> | |
using with = typename BuilderType<typename AddType<T, TypeList<Types...> >::type>::TypeAdder; | |
int value; | |
}; | |
struct A { | |
int a; | |
}; | |
struct B { | |
int b; | |
}; | |
int main() | |
{ | |
TypeAdder<> | |
::with<A> | |
::with<B> b; | |
b.value = 0; | |
return b.value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment