简体   繁体   中英

What happeneds to temporary variables when a macro expands to nothing?

#ifdef _DEBUG
// calls appropriate functions for message logging
#define LOGMESSAGE( stdStr ) gLogger.LogMessage( stdStr, __FILE__, __LINE__ );
// calls appropriate function for success logging
#define LOGSUCCESS( stdStr ) gLogger.LogSuccess( stdStr, __FILE__, __LINE__ );
// calls appropriate function for error logging
#define LOGFAILURE( stdStr ) gLogger.LogFailure( stdStr, __FILE__, __LINE__ );
#endif

#ifdef NDEBUG
// does nothing in release mode
#define LOGMESSAGE( stdStr )
// does nothing in release mode
#define LOGSUCCESS( stdStr )
// Logs failures in release mode
#define LOGFAILURE( stdStr ) gLogger.LogFailure( stdStr, __FILE__, __LINE__ );
#endif

Say I call the macros like the following

if ( SomeFunc() )
    {
    LOGSUCCESS("Success calling SomeFun()");
    }
else
    {
    LOGFAILURE("Failure calling SomeFun()");
    }

In release mode LOGSUCCESS is blank so does that mean "Success calling SomeFunc()" string isn't compiled into the code and won't exist in the compiled code, or is that left over, but the macro doesn't do anything with it?

EDIT: I mean does it effectively leave the code like this in release mode?

if ( SomeFunc() )
    {
    "Success calling SomeFun()";
    }
else
    {
    gLogger.LogFailure("Failure calling SomeFun()", __FILE__, __LINE__ );
    }

or

if ( SomeFunc() )
    {

    }
else
    {
    gLogger.LogFailure("Failure calling SomeFun()", __FILE__, __LINE__ );
    }

It will be expanded to -

if ( SomeFunc() )
    {
; //Remember the semicolon here
    }
else
    {
    gLogger.LogFailure("Failure calling SomeFun()", __FILE__, __LINE__ );
    }

The macro resolution is a pre-compile time activity, which just replaces the definition into the code before compilation starts.

You have not associated any "meaning" to your debug dynamic information in the macro - so it is not used at all.

To put it in simple term, macro is just a "search and replace" kind of activity during pre-compilation. Now since the str is not defined in the macro, it is not "used". Since it is a kind of pre-compile time activity,the question of temporary variable does not arise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM