Search code examples
c++pragma

msvc pragma warning omitting 'default' in cpp file


I have some pragma warning (disable : xxx) in cpp files (not headers):

now if we omit pragma warning (default : xxx) in same cpp file, does that warning stay disabled across all cpp files in project or just in this cpp file?

also if we compile multiple projects, does that disabled warning affect all projects? assuming pragma in cpp file only not in headers.

for example, I have:

#ifdef NDEBUG   // In release build using CrtDebug functions has no effect!
#define CRT_DBG_REPORT(...) 0
#pragma warning (disable : 6326)    // Potential comparison of a constant with another constant
#pragma warning (disable : 26814)   // The const variable can be computed at compile time
#pragma warning (disable : 26477)   // Use nullptr rather than 0 or NULL
#pragma warning (disable : 4127)    // conditional expression is constant
#pragma warning (disable : 4100)    // unreferenced formal parameter
#else
#define CRT_DBG_REPORT _CrtDbgReportW
#endif // NDEBUG

I would like to omit setting back to 'default' but ensure it only disables the warnings for this cpp file.

edit

thanks to comment section suggestion...

If we enable Unity build, what is the behavior of "undefauled" (only disabled) cpp pragmas? Project properties -> Advanced -> Unity.


Solution

  • If the warnings are disabled in the cpp file, they will only affect the lines underneath the pragma (they won't affect other compilation units). Unity builds would probably cause an issue though (at a guess, can't test this right now).

    Generally speaking, if you are enabling/disabling warnings via pragmas, this is probably the way to go:

    #define PUSH_DISABLE_WARNINGS \
      __pragma(warning(push)) \
      __pragma(warning(disable : 6326)) \
      __pragma(warning(disable : 26814)) \
      __pragma(warning(disable : 26477)) \
      __pragma(warning(disable : 4127)) \
      __pragma(warning(disable : 4100))
    
    #define POP_DISABLE_WARNINGS \
      __pragma(warning(pop))
    

    and then later on...

    PUSH_DISABLE_WARNINGS
    
    /* warnings will only be disabled here */
    
    POP_DISABLE_WARNINGS