2015-01-08 55 views
0

我正在寫一元函數MultipartitionWithUnaryPredicates,形式麻煩和語法模板,模板,模板

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

,這樣的類型的模板包將一元謂詞的包UnaryPredicates...被multipartitioned使用,按照一元謂詞的順序列出分區。如果你不知道我在說什麼,只是檢查出的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...> {}; 

現在一切正常 - 使用模板模板模板,這是我必須說,看起來相當我的第一次醜陋。

回答

2

JoinSpecial的定義中,Pack是一個類模板。然後,在指定模板參數爲Join時,您有typename Pack::headtypename Pack::tail。但Pack是一個模板,而不是類—您需要爲Pack提供模板參數。

+0

好了,確切的語法時才我需要的是'模板<模板類> P類,類型名包,模板類強的鬆,模板類... TS> 結構JoinSpecial:加入 :: head,typename MultipartitionWithUnaryPredicates :: tail,Ts ...> :: type> {};'Whew!模板,模板,模板。 – prestokeys