Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code allowed to compile under Visual Studio 2013?

Here is a very simple C++11 program that tests out the use of the final keyword to prevent a class from being subclassed:

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

private:
   T t;
};

class Derived : public Base<int> {};

int main(int, char **)
{
   Derived d;
   return 0;
}

If I try to compile the above program under Mac OS X (Clang), I get the expected error messages:

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

However, if I compile the same code under Windows using Visual Studio 2013, it compiles with no errors. If I make the Base class non-templated, however, Visual Studio does recognize the final keyword and emits an error.

Is this a bug in Visual Studio 2013, or am I missing some nuance about how the final keyword is allowed/expected to operate in conjunction with templated classes?

like image 632
Jeremy Friesner Avatar asked Apr 17 '15 22:04

Jeremy Friesner


People also ask

Can you compile code in Visual Studio?

Open your C++ code file in Text Editor, then use shortcut Ctrl+Alt+N , or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.

How do I change the compiler code in Visual Studio?

In Visual StudioIn the left pane, select Configuration Properties, C/C++ and then choose the compiler option category. The topic for each compiler option describes how it can be set and where it is found in the development environment. For more information and a complete list of options, see MSVC compiler options.

How do I compile a solution in Visual Studio?

To compile multiple C++ project items In Solution Explorer, choose multiple files that can be compiled, open the shortcut menu for one of those files, and then choose Compile, or press Ctrl+F7. If the files have dependencies, the files will be compiled in dependency order.


1 Answers

It is indeed a bug.

N4296, [class]/p3:

If a class is marked with the class-virt-specifier final and it appears as a base-type-specifier in a base-clause (Clause 10), the program is ill-formed.

like image 148
David G Avatar answered Oct 05 '22 23:10

David G