覆盖说明符作为模板参数 - 它有效吗?

Wer*_*mus 12 c++ compiler-bug c++11

我有以下代码不能在Visual C++ 2015下编译,但在GCC 4.8.4下编译.我想知道哪个是对的?有问题的代码如下:

template <class T> class ATemplate;
template <class R, class A1>
    struct ATemplate<R(A1)>{ };

int main()
{
    ATemplate<void(int)> x;
//    ATemplate<void(int)override> y; //---Does not compile!!!
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在下面(或const)使用override作为说明符是错误的.GMock库中存在类似的代码,其中宏扩展用于生成模板参数(包括覆盖)以及实际的函数签名.

删除注释掉的行时,Visual C++ 2015会产生以下错误:

x.cpp(11): error C2062: type 'int' unexpected
x.cpp(11): error C2976: 'ATemplate': too few template arguments
x.cpp(4): note: see declaration of 'ATemplate'
x.cpp(11): error C2079: 'y' uses undefined class 'ATemplate'
Run Code Online (Sandbox Code Playgroud)

下面的答案之一提到覆盖在自由函数的上下文中是没有意义的(有效点) - 这是否意味着GCC在这里是错误的.在这种情况下,const说明符也没有意义(对于自由函数),但仍允许(通过VC++)??? 此外,它提到虚拟说明符应仅存在于声明中 - 这与此情况没有区别(因为不存在定义).对于virtual关键字,可以在派生中省略,因为它对代码是否编译没有区别,但对于覆盖情况,它不是好的,因为它会产生很大的不同.

当使用ReturnType(ArgType arg)...可能的const或override说明符作为宏参数(如GMock那样)时,VCC施加的限制导致此代码无法编译(显然也是Clang的情况).哪个是对的?

标准没有说明在这种情况下(模板参数的上下文?)不应该使用覆盖说明符,是吗?

n. *_* m. 8

这是一个g ++错误.

该标准在两个产品中允许virt-specifier-sec:在函数定义(仅用于virtual 成员函数定义)和成员声明符中.你的背景既不是.

GCC错误行为的简短演示:

void foo(void) override;          // g++ rejects with message: 
                                  //  virt-specifiers in 'foo' 
                                  //  not allowed outside a class definition
void (*bar)(void) override;       // g++ erroneously accepts
typedef void baz(void) override;  // g++ erroneously accepts
Run Code Online (Sandbox Code Playgroud)


oo_*_*uel 5

根据标准,override说明符是上下文敏感的,只有成员函数声明之后使用时才具有特殊含义; 否则,它不是保留关键字.

所以我想说,你的第二个例子中的代码似乎毫无意义.

我尝试用gcc-5.1.0(带有-S标志)编译你的两个例子,它们导致完全相同的程序集.

它不编译clang-3.7.0导致以下错误:

test.cpp:11:23: error: expected '(' for function-style cast or type construction
Run Code Online (Sandbox Code Playgroud)

实际上,这意味着你不应该以这种方式使用覆盖.