Check for arguments type in a variadic template declaration

前端 未结 3 1428
终归单人心
终归单人心 2020-12-19 15:22

I got a plain variadic template declaration, just like the classic one:

template 
class VariadicTemplate;

What

3条回答
  •  悲哀的现实
    2020-12-19 15:37

    struct Foo {};
    
    #include 
    
    template
    struct are_same : std::true_type
    {};
    
    template
    struct are_same
        : std::integral_constant{} && are_same{}>
    {};
    
    template
    class VariadicTemplate
    {
        static_assert(are_same{}, "a meaningful error message");
    };
    
    int main()
    {
        VariadicTemplate v0{}; (void)v0;
        VariadicTemplate v1{}; (void)v1;
    }
    

    But something tells me you want to know if the arguments are all specializations of a class template Foo:

    template
    struct Foo {};
    
    #include 
    
    template class T, class U>
    struct is_template_of
    {
        template
        static std::true_type test(T*);
    
        static std::false_type test(...);
    
        constexpr static bool value = decltype(test((U*)nullptr)){};
    };
    
    template class T, class...>
    struct is_template_of_N : std::true_type
    {};
    
    template class T, class U, class... TT>
    struct is_template_of_N
        : std::integral_constant::value
                                       && is_template_of_N{}>
    {};
    
    template
    class VariadicTemplate
    {
        static_assert(is_template_of_N{},
                      "a meaningful error message");
    };
    
    int main()
    {
        VariadicTemplate, Foo> v0; (void)v0;
        VariadicTemplate, int> v1; (void)v1;
    }
    

提交回复
热议问题