现代" C ++,我有一个类型列表:
template <typename... T> struct TypeList {};
我想根据谓词拆分类型列表,例如std::is_floating_point
。更确切地说,我的完整工作示例是:
#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>();
}
打印:
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>]
我的问题:您是否知道可能只使用C ++(C ++ 17没有问题)和STL的更短/更优雅解决方案? (我不想使用像Boost,Hana这样的辅助库。)。
(我的动机:我不想错过一两行/超级优雅的解决方案,因为我会在其他地方广泛使用此功能)
我目前的实施是:
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;
};
仅仅是为了好奇,一个指向TypeList的历史指针(Andrei Alexandrescu,2002年2月1日):http://www.drdobbs.com/generic-programmingtypelists-and-applica/184403813
答案 0 :(得分:4)
这样的事情可能会更简单/更短
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<>{} ));
};
答案 1 :(得分:1)
我不会说以下方式更好或更优雅。
它与众不同,这是我的方式。
仅使用可变参数模板类的特化;没有功能。
也适用于C ++ 11。
希望这个例子有所帮助。
apply plugin: 'com.android.application'
repositories {
maven { url "https://jitpack.io" }
}
android {
...
}
dependencies {
....
compile 'com.github.fcannizzaro:material-stepper:1.0.2'
}