为什么允许在Visual Studio 2013下编译此代码?

Jer*_*ner 18 c++ templates final visual-studio-2013

这是一个非常简单的C++ 11程序,它测试了final关键字的使用,以防止类被子类化:

template<class T> class Base final
{
public:
   Base() {}

private:
   T t;
};

class Derived : public Base<int> {};

int main(int, char **)
{
   Derived d;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试在Mac OS X(Clang)下编译上述程序,我会收到预期的错误消息:

jeremy-friesners-mac-pro-3:~ jaf$ g++ -std=c++11 ./temp.cpp
./temp.cpp:10:28: error: base 'Base' is marked 'final'
   class Derived : public Base<int> {};
                       ^
./temp.cpp:1:29: note: 'Base' declared here
   template<class T> class Base final
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用Visual Studio 2013在Windows下编译相同的代码,它将编译没有错误.Base但是,如果我使该类非模板化,Visual Studio会识别该final关键字并发出错误.

这是Visual Studio 2013中的错误,还是我错过了关于如何final允许/期望关键字与模板化类一起操作的一些细微差别?

0x4*_*2D2 18

这确实是一个错误.

N4296,[class]/p3:

如果一个类用class-virt-specifier 标记,final并且它在base-clause(第10章)中显示为base-type-specifier,那么该程序就是格式错误的.


Jer*_*fin 13

我相信这是VS 2013中的一个错误.

对于它的价值,VS 2015 CTP的编译器可以做到(我认为)你可能期望的:

test.cpp(10): error C3246: 'Derived': cannot inherit from 'Base<int>' as it has been declared as 'final'
Run Code Online (Sandbox Code Playgroud)