C++ 为什么这个类的部分模板专门化代码不起作用?

C++ 为什么这个类的部分模板专门化代码不起作用?,c++,templates,visual-studio-2015,C++,Templates,Visual Studio 2015,我想Test使用类测试的第二个专门化。 但它似乎不起作用。我做错了什么 这是有效代码: >test.cpp(34): error C2976: 'Test' : too few template arguments >test.cpp(13): see declaration of 'Test' >test.cpp(34): error C2955: 'Test' : use of class template requires template argument list &

我想
Test
使用类测试的第二个专门化。 但它似乎不起作用。我做错了什么

这是有效代码:

>test.cpp(34): error C2976: 'Test' : too few template arguments
>test.cpp(13): see declaration of 'Test'
>test.cpp(34): error C2955: 'Test' : use of class template requires template argument list
>test.cpp(13): see declaration of 'Test'
#包括
结构空类型{};
模板
课堂测试
{
公众:
typedef int类型;
};
模板
类测试
{
公众:
typedef无符号字符类型;
};
模板
类测试
{
公众:
typedef双字型;
};
int main()
{
typedef测试::Type TargetType;
printf(“%s\n”,typeid(TargetType).name());
返回0;
}

请注意,由于使用了c++11和可变模板,您可以

#include <typeinfo>

struct NullType {};

template < typename T0 = NullType, typename T1 = NullType, typename T2 = NullType>
class Test
{
public:
    typedef int Type;
};

template< typename T0, typename T1 >
class Test< T0, T1, NullType >
{
public:
    typedef unsigned char Type;
};

template< typename T0 >
class Test< T0, NullType >
{
public:
    typedef double Type;
};

int main()
{
    typedef Test<int, int>::Type TargetType;

    printf("%s\n", typeid(TargetType).name());

    return 0;
}
模板结构测试;
模板
结构测试
{
使用Type=int;
};
模板
结构测试
{
使用类型=无符号字符;
};
模板
结构测试
{
使用类型=双;
};
int main()
{
使用TargetType=Test::Type;
//编译时检查:
静态断言(std::is_same::value,“意外类型”);
//运行时检查:

std::cout ok,我理解,部分模板专门化只是提供了一个特殊的类实现,当我们使用它时,我们必须提供所有的模板参数,所以在这种情况下,我可以设置默认类的默认参数。它会很好地工作:
#include <typeinfo>

struct NullType {};

template < typename T0 = NullType, typename T1 = NullType, typename T2 = NullType>
class Test
{
public:
    typedef int Type;
};

template< typename T0, typename T1 >
class Test< T0, T1, NullType >
{
public:
    typedef unsigned char Type;
};

template< typename T0 >
class Test< T0, NullType >
{
public:
    typedef double Type;
};

int main()
{
    typedef Test<int, int>::Type TargetType;

    printf("%s\n", typeid(TargetType).name());

    return 0;
}
template <typename ... Ts> struct Test;

template <typename T0, typename T1, typename T2>
struct Test<T0, T1, T2>
{
    using Type = int;
};

template <typename T0, typename T1>
struct Test<T0, T1>
{
    using Type = unsigned char;
};

template <typename T0>
struct Test<T0>
{
    using Type = double;
};

int main()
{
    using TargetType = Test<int, int>::Type;
    // Compile time check:
    static_assert(std::is_same<unsigned char, TargetType>::value, "unexpected type");
    // runtime check:
    std::cout << typeid(TargetType).name() << std::endl;
}