检查类是否具有模板特化(使用 bool 或 int 等模板参数)

Phi*_*ZXX 6 c++ templates type-traits variadic-templates c++17

基于如何判断模板类型是否是模板类的实例?检查类是否是模板专业化?我创建了以下变体来检查MyClass1, MyClass2or 的特定实例MyClass3

template <class T, template <class...> class Template>
constexpr bool is_instance_of_v = false;

template <template <class...> class Template, class... Args>
constexpr bool is_instance_of_v<Template<Args...>, Template> = true;

template<class T> struct MyClass1 { };
template<class T, class B> struct MyClass2 { };
template<class T, bool B> struct MyClass3 { };


int main(int argc, char* argv[])
{
    constexpr bool b1 = is_instance_of_v<MyClass1<float>, MyClass1>;
    constexpr bool b2 = is_instance_of_v<MyClass1<float>, MyClass2>;
    // constexpr bool b3 = is_instance_of_v<MyClass1<float>, MyClass3>;  // <-- does not compile

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,代码b3不编译并给出以下错误:

error C3201: the template parameter list for class template 'MyClass3' does not match the 
                 template parameter list for template parameter 'Template'
error C2062: type 'unknown-type' unexpected
Run Code Online (Sandbox Code Playgroud)

似乎这是因为bool参数 fromMyClass3不是 a class,因此无法通过template <class...> class Template.

有没有办法这么解决这个问题,它的作品模板参数任何列表(不只是class,而且boolint等)?

Jar*_*d42 3

没有通用模板来处理类型参数和非类型参数(以及模板模板参数)。