10

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.

3
  • 3
    Related: stackoverflow.com/questions/5642367/… ?
    – hellow
    Aug 7, 2018 at 8:32
  • It does compile. I think it simply does not work on VS2015. Look here, it says you need the /permissive- flag which VS2015 does not take. So the answers are not viable solutions, but viable answers never the less I am afraid. I would be happy to be proven wrong.
    – user4290866
    Aug 8, 2018 at 7:10
  • Which compiler version are you using? I found this where it says that C4596 is introduced in version 19.00.24215.1. Compiler explorer features an older compiler, so this might be a reason for it not working.
    – user4290866
    Aug 8, 2018 at 7:21

3 Answers 3

6

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.

4
  • I guess in VS2017 they'd encourage to use /Za for standard compliant code?
    – sandthorn
    Aug 7, 2018 at 9:26
  • 1
    " cannot find the right place in the standrad" - that's because the Standard mostly describes what's legal instead of describing what's illegal. This particular format is illegal because it's not legal. And it's not legal because there is no grammar rule that matches it.
    – MSalters
    Aug 7, 2018 at 11:11
  • @MSalters Alright, needed someone firm in the standard ;) Thanks
    – user4290866
    Aug 7, 2018 at 11:31
  • Yeah, I tried /permissive- but vs2k15u3 said it didn't know about the flag. /Ze said it was deprecated and refused to flag the invalid prefix as an issue anyway. For now, it seems it's not possible. Bugger, we've just gone from vs2k10 to vs2k15, now we'll have to make a quick leap to vs2k17 (though this transition should be easier, I hope).
    – paxdiablo
    Aug 8, 2018 at 0:41
2

look for C4596 int the following article:

https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/

//Use of qualified names in member declarations


struct A {
    void A::f() { } // error C4596: illegal qualified name in member declaration
                    // remove redundant 'A::' to fix
};
1
  • 1
    Please always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Also see here for guidance on link-only answers. Aug 7, 2018 at 9:08
0

Warning Level documentation

/wennnn: Treats the compiler warning that is specified by nnnn as an error.

So according to @p-i's comment above, for warning C4596, adding parameter /we4596 does the trick then.

<source>(5): error C4596: 'Return42': illegal qualified name in member declaration
Compiler returned: 2

While adding /w14596 only issues as warning, if you prefered.

<source>(5): warning C4596: 'Return42': illegal qualified name in member declaration
Compiler returned: 0

As @p-i comments below, this is for VS2017++ only.

3
  • This is true, but I think this does not work for MSVC 2015, can somebody varify? Am I using it wrong?
    – user4290866
    Aug 7, 2018 at 9:08
  • 1
    @p-i I'd like confirm that not working on MSVC 2015 for both /we4596 and /w14596
    – sandthorn
    Aug 7, 2018 at 9:15
  • Thanks for trying
    – user4290866
    Aug 7, 2018 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.