Search code examples
c++visual-c++compiler-errorsvisual-studio-2019compiler-warnings

Treat C4596 as Warning


Using the compiler option /Wall with VS 2019 16.8.6, I'm getting a C4596;

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

for the following test program:

struct S {
    int S::i;
};
int main() {}

Here's a godbolt.

  • The error shows up for all VS 2019 version available there with /Wall.
  • VS 2017 15.9.33 shows the same behavior.
  • VS 2015 doesn't show it.
  • VS 2017 and 2019 even show it when all warnings are disabled with /w, however specifically disabling 4596 really disables it.

Is this a bug or am I doing/reading something wrong?`

Is there a way to treat some, especially this, error as warning (not the other way round)?


Solution

  • There is a (somewhat weird) way to 'reset' this warning to 'normal' mode (i.e. to not treat it as an error); this involves using the #pragma warning(n:xxxx) directive, as shown below:

    #pragma warning(4:4596)
    struct S {
        int S::i; // With that #pragma, this now generates 'just' a warning
    };
    

    warning C4596: 'i': illegal qualified name in member declaration

    Not being party to the design of the MSVC compiler, I can't really say any more about why this happens. However, more information on adjusting its warnings using #pragma directives can be found here.

    Also, possibly of interest, is that the clang-cl compiler (used from within Visual Studio) generates just a warning, with or without that pragma:

    warning : extra qualification on member 'i' [-Wmicrosoft-extra-qualification]