模板模板模板的语法问题

时间:2015-01-08 17:33:19

标签: c++ templates c++11 variadic

我正在编写一个元函数MultipartitionWithUnaryPredicates,用于

形式
MultipartitionWithUnaryPredicates<Pack<Args...>, UnaryPredicates...>::type

这样一个类型的模板包将由一元谓词的包UnaryPredicates...进行多分区,其中的分区按列出的一元谓词的顺序排列。如果你不确定我的意思,只需查看下面代码中的main()(这是正常的):

#include <iostream>
#include <type_traits>
#include <typeInfo>

template <template <typename> class, typename, typename, typename> struct Helper;  

template <template <typename> class UnaryPredicate, template <typename...> class P, typename... Types1, typename... Types2>
struct Helper<UnaryPredicate, P<>, P<Types1...>, P<Types2...>> {
    using type = P<Types1..., Types2...>;
    using head = P<Types1...>;
    using tail = P<Types2...>;
};

template <template <typename> class UnaryPredicate, template <typename...> class P, typename First, typename... Rest, typename... Types1, typename... Types2>
struct Helper<UnaryPredicate, P<First, Rest...>, P<Types1...>, P<Types2...>> : std::conditional<UnaryPredicate<First>::value,
        Helper<UnaryPredicate, P<Rest...>, P<Types1..., First>, P<Types2...>>,
        Helper<UnaryPredicate, P<Rest...>, P<Types1...>, P<Types2..., First>>
    >::type {};

template <typename, template <typename> class> struct PartitionWithUnaryPredicate;

template <template <typename> class UnaryPredicate, template <typename...> class P, typename... Ts>
struct PartitionWithUnaryPredicate<P<Ts...>, UnaryPredicate> : Helper<UnaryPredicate, P<Ts...>, P<>, P<>> {};

template <typename, template <typename> class...> struct MultipartitionWithUnaryPredicates;

template <typename Pack, template <typename> class UnaryPredicate>
struct MultipartitionWithUnaryPredicates<Pack, UnaryPredicate> : PartitionWithUnaryPredicate<Pack, UnaryPredicate> {};

template <typename, typename> struct Join;

template <template <typename...> class P, typename... Types1, typename... Types2>
struct Join<P<Types1...>, P<Types2...>> {
    using type = P<Types1..., Types2...>;
};

//template <template <typename, template <typename> class> class Pack, typename... Ts>
//struct JoinSpecial : Join<typename Pack::head, typename MultipartitionWithUnaryPredicates<typename Pack::tail, Ts...>::type> {};

template <typename Pack, template <typename> class First, template <typename> class... Rest>
struct MultipartitionWithUnaryPredicates<Pack, First, Rest...> : Join<typename PartitionWithUnaryPredicate<Pack, First>::head, typename MultipartitionWithUnaryPredicates<typename PartitionWithUnaryPredicate<Pack, First>::tail, Rest...>::type> {};
// The above can be improved, since PartitionWithUnaryPredicate<Pack, First> is being computed twice.

// -----------------------------------------------------------------------------------------------------------------------------------------------
// Testing:

template <typename...> struct Pack {};

template <typename Last>
struct Pack<Last> {
    static void print() {std::cout << typeid(Last).name() << std::endl;}
};

template <typename First, typename ... Rest>
struct Pack<First, Rest...> {
    static void print() {std::cout << typeid(First).name() << ' ';  Pack<Rest...>::print();}
};

struct Thing {};
struct Blob { Blob(Blob&&){} };  // Copy constructor deleted.
struct Object {};
enum MyEnum {x, y, z};
enum HerEnum {xx, yy, zz};

int main() {
    MultipartitionWithUnaryPredicates<Pack<int, Thing, double, short, Blob, char, MyEnum, long, Object, float, HerEnum>,
        std::is_integral>::type b;
    b.print();  // int short char long Thing double Blob MyEnum Object float HerEnum

    MultipartitionWithUnaryPredicates<Pack<int, Thing, double, short, Blob, char, MyEnum, long, Object, float, HerEnum>,
        std::is_integral, std::is_enum>::type c;
    c.print();  // int short char long MyEnum HerEnum Thing double Blob Object float

    MultipartitionWithUnaryPredicates<Pack<int, Thing, double, short, Blob, char, MyEnum, long, Object, float, HerEnum>,
        std::is_integral, std::is_enum, std::is_arithmetic>::type d;
    d.print();  // int short char long MyEnum HerEnum double float Thing Blob Object

    MultipartitionWithUnaryPredicates<Pack<int, Thing, double, short, Blob, char, MyEnum, long, Object, float, HerEnum>,
        std::is_integral, std::is_enum, std::is_arithmetic, std::is_member_pointer, std::is_copy_constructible>::type e;
    e.print();  // int short char long MyEnum HerEnum double float Thing Object Blob
}

问题在于尝试优化上述代码。

PartitionWithUnaryPredicate<Pack, First>

正在计算两次,我正在尝试定义仅使用它一次的JoinSpecial。但我无法正确理解语法。

template <template <typename, template <typename> class> class Pack, typename... Ts>
struct JoinSpecial : Join<typename Pack::head, typename MultipartitionWithUnaryPredicates<typename Pack::tail, Ts...>::type> {};

无法编译。模板类型PartitionWithUnaryPredicate的类型为template <typename, template <typename> class> class,不是吗?

更新 感谢Angew的提示,我现在的语法正确:

template <template <typename, template <typename> class> class P, typename Pack, template <typename> class Pred, template <typename> class... Ts>
struct JoinSpecial : Join<typename P<Pack, Pred>::head, typename MultipartitionWithUnaryPredicates<typename P<Pack, Pred>::tail, Ts...>::type> {};

template <typename Pack, template <typename> class First, template <typename> class... Rest>
struct MultipartitionWithUnaryPredicates<Pack, First, Rest...> : JoinSpecial<PartitionWithUnaryPredicate, Pack, First, Rest...> {};

现在一切正常 - 我第一次使用模板模板模板,我必须说这看起来很难看。

1 个答案:

答案 0 :(得分:2)

JoinSpecial的定义中,Pack是一个类模板。然后,在指定Join的模板参数时,您有typename Pack::headtypename Pack::tail。但Pack是模板,而不是类 - 您需要为Pack提供模板参数。

相关问题