Body for pure virtual function in C ++ 17?

I tried to write a pure virtual function in the Base class, and I give it the body next to the definition , as shown in the code below, since I know that I should get a compilation error, but everything worked fine. Is this something new that is related to C ++ 17? (I used visual studio 2017)

class Base { public: virtual void virtual_func() { std::cout << "This a virtual function from BASE" << std::endl; }; virtual void pure_func() = 0 { std::cout << "This a PURE virtual function from BASE" << std::endl; }; }; 

thanks

+5
source share
1 answer

Pure virtual cannot be used with the definition. This was true in C ++ 11, and in C ++ 14:

10.4 / 2: ...
[Note. A function declaration cannot provide both a pure-specifier and a note to the end of the definition] [Example:

 struct C { virtual void f() = 0 { }; // ill-formed }; 

The same statement (in accordance with section 13.4 / 2 this time) remains true for C ++ 17 (N4659).

So, if your compiler accepts this code, this is definitely an error ( gcc 7.1 doesesnt )

+4
source

Source: https://habr.com/ru/post/1267165/


All Articles