Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most elegant way to split a C++ TypeList

In "modern" C++, I have a type list:

template <typename... T> struct TypeList {};

I want to split type list according to a predicate, for instance std::is_floating_point. To be more precise, my complete working example is:

#include <iostream>
#include <type_traits>

template <typename... T> struct TypeList {};

// SplitTypeList<> implementation defined at the end of this post...

template <typename T>
void printType()
{
  std::cout << "\n" << __PRETTY_FUNCTION__;
}

int main()
{
  struct A
  {
  };

  using typeList = TypeList<int, double, float, A, int>;

  using splited_typeList = SplitTypeList<std::is_floating_point, typeList>;

  using float_typeList = splited_typeList::predicate_is_true_typeList_type;
  using other_typeList = splited_typeList::predicate_is_false_typeList_type;

  printType<float_typeList>();
  printType<other_typeList>();
}

prints:

g++ -std=c++17 typeList.cpp -o typeList; ./typeList

void printType() [with T = TypeList<double, float>]
void printType() [with T = TypeList<int, main()::A, int>]

my question: have you an idea of a possible shorter/more elegant solution that only uses C++ (no problem with C++17) and STL? (I do not want to use an auxiliary lib like Boost, Hana...).

(My motivation: I do not want to miss a one or two lines / super elegant solution, as I will use this functionality extensively in other places)


My current implementation is:

namespace Details
{
  template <template <typename> class PREDICATE,
            typename... TYPELIST_PREDICATE_IS_TRUE,
            typename... TYPELIST_PREDICATE_IS_FALSE>
  constexpr auto splitTypeList(TypeList<TYPELIST_PREDICATE_IS_TRUE...>,
                               TypeList<TYPELIST_PREDICATE_IS_FALSE...>,
                               TypeList<>)
  {
    return std::make_pair(TypeList<TYPELIST_PREDICATE_IS_TRUE...>(),
                          TypeList<TYPELIST_PREDICATE_IS_FALSE...>());
  }

  template <template <typename> class PREDICATE,
            typename... TYPELIST_PREDICATE_IS_TRUE,
            typename... TYPELIST_PREDICATE_IS_FALSE,
            typename T,
            typename... TAIL>
  constexpr auto splitTypeList(TypeList<TYPELIST_PREDICATE_IS_TRUE...>,
                               TypeList<TYPELIST_PREDICATE_IS_FALSE...>,
                               TypeList<T, TAIL...>)
  {
    if constexpr (PREDICATE<T>::value)
    {
      return splitTypeList<PREDICATE>(
          TypeList<TYPELIST_PREDICATE_IS_TRUE..., T>(),
          TypeList<TYPELIST_PREDICATE_IS_FALSE...>(),
          TypeList<TAIL...>());
    }
    else
    {
      return splitTypeList<PREDICATE>(
          TypeList<TYPELIST_PREDICATE_IS_TRUE...>(),
          TypeList<TYPELIST_PREDICATE_IS_FALSE..., T>(),
          TypeList<TAIL...>());
    }
  }

  template <template <typename> class PREDICATE, typename... T>
  constexpr auto splitTypeList(TypeList<T...>)
  {
    return splitTypeList<PREDICATE>(
        TypeList<>(), TypeList<>(), TypeList<T...>());
  }
}

template <template <typename> class PREDICATE, typename TYPELIST>
struct SplitTypeList;

template <template <typename> class PREDICATE, typename... TAIL>
struct SplitTypeList<PREDICATE, TypeList<TAIL...>>
{
  using pair_type = decltype(
      Details::splitTypeList<PREDICATE>(std::declval<TypeList<TAIL...>>()));
  using predicate_is_true_typeList_type = typename pair_type::first_type;
  using predicate_is_false_typeList_type = typename pair_type::second_type;
};

Just for curiosity, a historical pointer to TypeList (Andrei Alexandrescu, February 01, 2002): http://www.drdobbs.com/generic-programmingtypelists-and-applica/184403813

like image 999
Picaud Vincent Avatar asked Oct 20 '17 15:10

Picaud Vincent


1 Answers

something like this may be somewhat simpler/shorter

template< bool, template<typename> class, class... Vs >
auto FilterImpl( TypeList<>, TypeList<Vs...> v ) { return v; }

template< bool Include, template<typename> class P, class T, class... Ts, class... Vs >
auto FilterImpl( TypeList<T,Ts...>, TypeList<Vs...> ) { return FilterImpl<Include,P>(
  TypeList<Ts...>{} ,
  std::conditional_t< Include == P<T>::value, TypeList<T,Vs...>, TypeList<Vs...> >{}
  ); }

template <template <typename> class PREDICATE, typename TYPELIST>
struct SplitTypeList
{
  using predicate_is_true_typeList_type = decltype(FilterImpl<true,PREDICATE>( TYPELIST{}, TypeList<>{} ));
  using predicate_is_false_typeList_type = decltype(FilterImpl<false,PREDICATE>( TYPELIST{}, TypeList<>{} ));
};
like image 151
Massimiliano Janes Avatar answered Nov 08 '22 12:11

Massimiliano Janes