Home > Enterprise >  C Debug macro failing compile in release mode
C Debug macro failing compile in release mode

Time:06-17

I'm using visual studio (2019) and I have this macro:

#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#endif

My log function this wraps around is:

template<typename... args>
static void Log(args && ... inputs)
{
    std::osyncstream bout(std::cout);
    bout << ENGINE_TAG;
    ([&]()  {
        bout << inputs;
    } (), ...);

    bout << "\n";
}

Obviously this works fine when I'm in debug mode, but when I use release mode I get:

Error   C3861   'EngineLog': identifier not found

I'm unsure why the compiler takes issue here, I was told it would remove the macro from the code when not defined but it seems not to be doing that.

CodePudding user response:

if _DEBUG is not defined, then EngineLog is an unresolved name. You need to add a definition for the other case.

#if _DEBUG
    #define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
    #define EngineLog(vars, ...) (0 && Log(vars, ##__VA_ARGS__))
#endif

The syntax used here prevents any potential warnings about the variadic arguments being unused while at the same time preventing both the function from being called and its parameters being evaluated.

CodePudding user response:

Looks like you need a #else preprocessor case to define out the macro call with an empty expansion:

#if _DEBUG
    #define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
    #define EngineLog(vars, ...)
#endif
  •  Tags:  
  • c
  • Related