Compiler Warning (level 1) C5105

macro expansion producing 'defined' has undefined behavior

Remarks

The preprocessor detected a defined operator in the output of a macro expansion. If a defined operator appears as the result of a macro expansion, the C standard specifies the behavior as undefined. The C5105 warning is a portability and standards conformance warning, issued because other conformant compilers may have different behavior. To resolve this issue, move the defined operator out of the macro, or suppress warning C5105.

Microsoft-specific behavior: The MSVC compiler evaluates the defined operator normally, even under /permissive-.

This warning is new in Visual Studio 2017 version 15.8. It's only generated by the new standards-conformant preprocessor, specified by the /experimental:preprocessor compiler option.

To turn off the warning without code changes

You can turn off the warning for a specific line of code by using the warning pragma, #pragma warning(suppress : 5105). You can also turn off the warning within a file by using the warning pragma, #pragma warning(disable : 5105). You can turn off the warning globally in command-line builds by using the /wd5105 command-line option.

To turn off the warning for an entire project in the Visual Studio IDE:

  1. Open the Property Pages dialog for your project. For information on how to use the Property Pages dialog, see Property Pages.
  2. Select the Configuration Properties > C/C++ > Advanced page.
  3. Edit the Disable Specific Warnings property to add 5105. Choose OK to apply your changes.

Example

This sample program shows how to generate warning C5105, and how to fix it.

// C5105.cpp
// To demonstrate the warning,
// compile by using: cl /EHsc /experimental:preprocessor /DTEST C5105.cpp
// To fix the warning, change the DEFINED_TEST
// definition to the commented version.

#include <iostream>

#define DEFINED_TEST defined TEST
//#if defined TEST
//#define DEFINED_TEST 1
//#else
//#define DEFINED_TEST 0
//#endif

int main()
{
#if DEFINED_TEST  // C5105
    std::cout << "TEST defined\n";
#else
    std::cout << "TEST not defined\n";
#endif
}