C++ 98 中的部分模板专业化?

问题描述

C++ 98 是否支持部分模板规范?

以下代码在 C++ 11 下编译良好,但在 Visual C++ 6.0 中无法编译。
所以我想知道语法是否需要略有不同,或者只是不支持

#include <iostream>
#include <string>

template <typename A,typename B> class Foo
{
public:
    static void bar(A a,B b)
    {
        std::cout << "A";
    };
};

template <typename A> class Foo<A,std::string>
{
public:
    static void bar(A a,std::string b)
    {
        std::cout << "B";
    };
};

template <typename B> class Foo<std::string,B>
{
public:
    static void bar(std::string a,B b)
    {
        std::cout << "C";
    };
};

template <> class Foo<std::string,std::string>
{
public:
    static void bar(std::string a,std::string b)
    {
        std::cout << "D";
    };
};

int main(int argc,char* argv[])
{
    Foo<int,int>::bar(12,42);
    Foo<int,std::string>::bar(12,"");
    Foo<std::string,int>::bar("",42);
    Foo<std::string,std::string>::bar("","");

    return 0;
}

错误信息:

Compiling...
Test.cpp
C:\test\test.cpp(20) : error C2989: 'Foo<A,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >' : template class has already been defined as a non-template class
C:\test\test.cpp(20) : error C2988: unrecognizable template declaration/deFinition
C:\test\test.cpp(29) : error C2989: 'Foo<class std::basic_string<char,class std::allocator<char> >,A>' : template class has already been defined as a non-template class
C:\test\test.cpp(29) : error C2988: unrecognizable template declaration/deFinition
Error executing cl.exe.
 
Test.obj - 4 error(s),0 warning(s)

解决方法

Microsoft Visual C++ 6.0 不支持部分模板特化。它是一个 known bug

有关 VC++6.0 的更多已知标准合规性问题,请参阅 here


存档链接,因为这些知识库文章似乎已从 Microsoft 数据库中删除

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...