How can I curry variadic template template parameters?

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

Variadic template template parameters accept any template:

template
struct Test1 {
    using type = int;
};

template

        
2条回答
  •  旧巷少年郎
    2021-02-08 19:36

    After some problems of my own, I came up with this solution which will work for any template class (also the ones you provide in your post).
    The core of this solution is is_valid_specialization which is used as the condition on whether or not the currying process may be considered complete:

    #include 
    #include 
    
    template class C, typename... T>
    struct is_valid_specialization {
        typedef struct { char _; } yes;
        typedef struct { yes _[2]; } no;
    
        template class D>
        static yes test(D*);
        template class D>
        static no test(...);
    
        constexpr static bool value = (sizeof(test(0)) == sizeof(yes));
    };
    
    namespace detail {
    
        template class BeCurry, bool = false, typename... S>
        struct Currying {
    
            template
            using apply = Currying::value, S..., T...>;
        };
    
        template class BeCurry, typename... S>
        struct Currying {
    
            template
            using apply = Currying::value, S..., T...>;
    
            using type = typename BeCurry::type;
        };
    }
    
    template class BeCurry>
    using Currying = detail::Currying::value>;
    
    template
    struct Test1 { using type = int; };
    
    template
    struct Test2 { using type = char*; };
    
    template
    struct Test3 { using type = double; };
    
    using curry  = Currying;
    using curry2 = Currying;
    using curry3 = Currying;
    
    template
    void pretty_print(T) {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    
    int main() {
        pretty_print(typename curry::apply::type{});
        pretty_print(typename curry2::apply::apply::type{});
        pretty_print(typename curry3::type{});
    }
    

    Output on ideone

提交回复
热议问题