Search code examples
c++visual-studionamespacesstandards-compliance

How to deal with math.h pollution in Visual Studio C++?


In Visual Studio 2012, I'm unable to declare certain names as global identifiers because they're already declared in math.h. Legacy issues makes it inconvenient for me to rename the identifiers in the source code. What are the options besides renaming?

#include "stdafx.h"
// iostream includes math.h which declares the following
_CRT_NONSTDC_DEPRECATE(_y1) _CRTIMP double  __cdecl y1(_In_ double _X);

int y1; // error - y1 is already declared

void Main()
{
    return;
}

Bonus question: Is Visual Studio 2012 handling this in a conforming manner?


Solution

  • Since this is C++, you should use a namespace for your own stuff, especially if you have global variables.

    #include "stdafx.h"
    
    namespace MyApp
    {
        int y1; // MyApp::y1
    }
    

    This way, you can rely on the using keyword, where you need to use your y1 variable without the namespace name:

    using MyApp::y1; // Now also y1