Matching variadic non-type templates

后端 未结 3 1902
予麋鹿
予麋鹿 2021-02-18 14:13

Let\'s say I have two structs, Foo and Bar:

template
struct Foo{};

template
struct Bar{};
         


        
3条回答
  •  难免孤独
    2021-02-18 14:49

    Here is a general C++14 solution that does not rely on manually specialized type traits or extending Foo and Bar.

    A template metafunction that obtains a type representing the class template of its argument type:

    namespace detail
    {
        // Type representing a class template taking any number of non-type template arguments.
        template  class U>
        struct nontype_template {};
    }
    
    // If T is an instantiation of a class template U taking non-type template arguments,
    // this has a nested typedef "type" that is a detail::nontype_template representing U.
    template 
    struct nontype_template_of {};
    
    // Partial specializations for all of the builtin integral types.
    template