MSVC:无法识别的模板声明/定义(使用 Clang/GCC 编译)

MSVC: unrecognizable template declaration/definition (compiles with Clang/GCC)

本文关键字:定义 使用 GCC 编译 Clang 声明 识别 MSVC      更新时间:2023-10-16

我尝试构建一个在OSX(clang)和Linux(GCC/Clang)上编译良好的库。我偶然发现了MSVC/Visual Studio 2017不理解的模板定义。我可以把它归结为:

#include "stdafx.h"
template<class T>
class Parent {
    class Child {
        friend class Parent;
    public:
        Child(const Child &r);
    };
};
template<class T>
Parent<T>::Child::Child(const Parent<T>::Child &r) {
    *this = r;
}
int main(int argc, char const *argv[]) {
    /* code */
    return 0;
}

这(仍然)会导致以下错误:

1>...consoleapplication1.cpp(13): error C2988: unrecognizable template declaration/definition
1>...consoleapplication1.cpp(13): error C2059: syntax error: '('
1>...consoleapplication1.cpp(13): error C2143: syntax error: missing ';' before '{'
1>...consoleapplication1.cpp(13): error C2447: '{': missing function header (old-style formal list?)

但(仍然)使用 Clang 或 GCC 在 OSX 上编译。 stdafx.hmain不是实际库代码的一部分,但已添加以允许在VS Windows命令行项目中使用它。

我在这里错过了什么?MSVC 在模板中的成员类或友元声明方面是否存在问题?

VC++的问题是参数类型没有被typename限定。[温度]/7:

[...]的定义之内 声明符 ID(关键字)后面的类模板的成员 引用以前的名称时不需要typename 声明类型或类的类模板的声明成员 模板。

声明符 id声明的第一个组件,因此参数子句中的任何成员类型都不需要以 typename 为前缀。