无法识别的模板声明

无法识别的模板声明

本文介绍了MSVC:无法识别的模板声明/定义(使用Clang/GCC编译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个在OSX(clang)和Linux(GCC/Clang)上都能正常编译的库.我偶然发现了MSVC/Visual Studio 2017无法理解的模板定义.我可以将其归结为:

I try to build a library which compiles just fine on OSX (clang) and Linux (GCC/Clang). I stumbled upon a template definition which MSVC/Visual Studio 2017 does not understand. I could boil it down to:

#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;
}

(仍然)导致以下错误:

This (still) results in the following error:

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.h 以及 main 都不是实际库代码的一部分,但已添加以允许在VS Windows命令行项目中使用它.

but (still) compiles on OSX with Clang or GCC. stdafx.h and also main are not part of the actual library code but have been added to allow using this in a VS Windows command line project.

我在这里想念什么?MSVC在模板中的成员类或朋友声明方面是否存在问题?

What do I miss here? Does MSVC have issues with member classes or friend declarations in templates?

推荐答案

VC ++的问题是参数类型不受 typename 限定. [temp.res]/7 :

VC++'s problem is the fact that the parameter type isn't qualified by typename. [temp.res]/7:

declarator-id declaration 的第一个组成部分,因此parameter子句中的任何成员类型都不必以 typename 作为前缀

The declarator-id is the first component of the declaration, hence any member type in the parameter clause need not be prefixed by typename.

这篇关于MSVC:无法识别的模板声明/定义(使用Clang/GCC编译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:38