How do I get VS2015 to rightly reject use of a class prefix in the class declaration?

  • A+
Category:Languages

I recently copy and pasted a C++ member function from my cpp file into the header and forgot to remove the prefix. So, in the cpp file, I had something like:

int MyClass::Return42() const { return 42; } 

and, in my header:

class MyClass { public:     int MyClass::Return42() const; }; 

Now I'm sure I've done that before and had the compiler complain bitterly that it was not allowed, requiring the removal of the class prefix in the header. In fact g++ 5.4.0 complains about it under Linux regardless of which ISO standard I target (11 through 17), using a single file (though -fpermissive will turn this into a warning rather than an error):

#include <iostream>  class MyClass { public:     int MyClass::Return42() const; };  int MyClass::Return42() const { return 42; }  int main() {     MyClass x;     std::cout << x.Return42() << '/n'; } 

But I find that my brand new install of VsPro15 appears to allow that.

How do I get Visual Studio to reject this invalid code, given I'd like my code to be portable across different platforms?


I am aware of the suggested solutions involving /permissive- and /Ze.

For the first, even with VS2k15 Update 3, entering /permissive- into the Project properties | C/C++ | Command Line | Aditional Options field, results in:

2>cl : Command line warning D9002: ignoring unknown option '/permissive-'

For the second, I see:

2>cl : Command line warning D9035: option 'Ze' has been deprecated and will be removed in a future release

but it compiles the errant code anyway.

So I don't believe that either is a viable solution.

 


As far as I know, you will not be able no make VS2015 warn about this. This document states that you need the /permissive- switch in order to get this specific warning. It also states that it should be included in VS2015. Maybe this is a compiler bug?

In VS2017 however, you are able to use the /permissive- switch, to make the compiler more standard conform (this is also a hint, that it is not standard conform, but I cannot find the right place in the standrad -- maybe someone can fill it in), see here. With this you get the foillowing warning:

error C4596: 'Return42': illegal qualified name in member declaration 

The warning you want can also be triggered using /we4596 to only enable this warning instead of all coming from a more standard conform compiler.

When I understand the documentation correctly, in VS2015 you can use the /Ze switch (in contrast to /permissive-), however, when I used the compiler-explorer correctly, this still does not warn, see here. The /Ze switch is also discussed in this SO question.

EDIT:

I found this where it says that Warning C4596 is introduced in the compiler version 19.00.24215.1. Compiler explorer currently features an older compiler (i.e. 19.00.24210), so this might be a reason for it not working.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: