【问题标题】:Partial Template Specialization in C++ 98?C++ 98 中的部分模板专业化?
【发布时间】:2021-08-26 02:40:26
【问题描述】:

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,struct std::char_traits<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)

【问题讨论】:

  • C++ 98 支持部分特化,但 VC++6 并不真正支持 C++98。它的虫子比蚁丘还多。为什么今天会有人使用它?
  • @n.1.8e9-where's-my-sharem。不幸的是,有很多遗留软件需要维护,而且如果不花费大量精力(和金钱),它们不一定适用于新版本。

标签: c++ templates template-specialization c++98 visual-c++-6


【解决方案1】:

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

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多