How can I curry variadic template template parameters?

前端 未结 2 1583
旧巷少年郎
旧巷少年郎 2021-02-08 18:46

Variadic template template parameters accept any template:

template
struct Test1 {
    using type = int;
};

template

        
2条回答
  •  孤街浪徒
    2021-02-08 19:39

    Simple solution is:

    template
        <
            template  class BeCurry,
            typename... Params
        >
    struct Currying
    {
        template 
        using curried = BeCurry;
    
        template 
        using type = typename curried::type;
    
        template 
        using apply = Currying;
    };
    

    But it does not work with templates like Test1 and Test2 due to compilation errors (under gcc, at least). A workaround for this problem looks like this:

    template
        <
            template  class BeCurry,
            typename... Params
        >
    struct Curry
    {
        using type = BeCurry;
    };
    
    template
        <
            template  class BeCurry
        >
    struct Curry
    {
        using type = BeCurry<>;
    };
    

    And now lines

    template 
    using curried = BeCurry;
    

    should be replaced with lines

    template 
    using curried = typename Curry::type;
    

    Example of using:

    #include 
    #include 
    
    template 
    void print_type(T t)
    {
        std::cout << typeid(t).name() << std::endl;
    }
    
    // ...
    
    print_type(Currying::type{});
    print_type(Currying::apply::type<>{});
    print_type(Currying::type{});
    print_type(Currying::apply::type{});
    print_type(Currying::apply::apply::type<>{});
    print_type(Currying::apply::type<>{});
    

    Full example at ideone.

提交回复
热议问题