Is there an equivalent of gcc's -Wshadow in visual C++

后端 未结 5 1940
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 09:50

-Wshadow will \"Warn whenever a local variable shadows another local variable.\". Is there an equivalent in Visual C++ (2008)? I tried /W4 but it didn\'t pick up on it. I al

5条回答
  •  [愿得一人]
    2020-12-17 10:26

    Visual Studio 2015 now warns about shadowed variables by default. Excerpt from http://blogs.msdn.com/b/vcblog/archive/2014/11/12/improvements-to-warnings-in-the-c-compiler.aspx follows:

    Shadowed variables A variable declaration "shadows" another if the enclosing scope already contains a variable with the same name. For example:

    void f(int x) 
    { 
        int y;
        { 
            char x; //C4457
            char y; //C4456
        } 
    }
    

    The inner declaration of x shadows the parameter of function f, so the compiler will emit: warning C4457: declaration of 'x' hides function parameter The inner declaration of y shadows the declaration of y in the function scope, so the compiler will emit: warning C4456: declaration of 'y' hides previous local declaration Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:

    void f(int x) 
    { 
        char x; //C2082
    }
    

    The compiler emits: error C2082: redefinition of formal parameter 'x'

提交回复
热议问题