Empty Functions

Started by
14 comments, last by Antheus 16 years, 9 months ago
Hiya, Does anyone know if MSVS 2005 will just omit any function calls to empty functions when it compiles a program? I know it's a bit of a strange question - the reason behind it is I'd like to take advantage of the OutputDebugString() function like so:

void DebugOut( const wchar_t *format, ... )
{

    #ifdef _DEBUG

    // use va_start() etc. to build the string

    OutputDebugString( msgbuf );

    #endif

}
I just want to check it won't cost me anything in a release build. Thanks for any help [smile]
Advertisement
You should be using #Ifdef _Debug on the calls to Debugout, not on the debug function. From my understanding there will still be a call to the function even though it is empty.

theTroll
An empty inline function is ignored, yes. If it isn't inline, the linker may choose to ignore it, but it's not certain.

The arguments to the function will still be evaluated if there is any chance that they have a side-effect.
OK, thanks. I can inline it, but I suppose there's no guarantee it'll actually be inlined by the compiler. Coudld you suggest another method?

I was going to use it like this:
try {    // all these may throw    pfnInitRenderer( ... );    CreateWindow( ... );    AdjustWindow( ... );        // etc...}catch( const wchar_t *pReasonForThrow ){    // this generates warning C4101 - uninitialized local variable    #ifdef _DEBUG    DebugOut( L"Error: \s. Renderer::Initialize() fails.\n" );    #endif}


I could disable the warning, but I'd much rather not have to, I guess I'm strange like that.

Thanks again.
Well, you are weird about that warning deal [smile] Have you corrected what was causing it? In practice, getting this warning because of a conditional compilation of a logging statement is bad news.
I prefer the word 'special' [smile] But more seriously...

Sorry - C4101 is an unreferenced local variable, it's caused because pReasonForThrow is never used in the release build. I guess I'll have to deal with an empty function call for now - at the end of the day, if an exception's been thrown anyway a tiny bit more overhead won't make a difference. It just seems a bit ugly.

Thanks for your help.
I'm assuming I'm well off-base here, but since the preprocessor is just straight text-replacement, couldn't you go with:

#ifdef _DEBUGtry {#endif    // all these may throw    pfnInitRenderer( ... );    CreateWindow( ... );    AdjustWindow( ... );        // etc...#ifdef _DEBUG}catch( const wchar_t *pReasonForThrow ){    DebugOut( L"Error: \s. Renderer::Initialize() fails.\n" );}#endif


and, outside debug mode, let the exception propagate (hopefully) to something that can handle it, rather than pretending to handle it -- which seems a bit messy, to me? Although, I guess, you then end up sprinkling '#ifdef'/'#endif' all over the place.

(Woah, that's one serious run-on sentence)
[TheUnbeliever]
or just:

#ifdef _DEBUG#define DebugWhatever( arguments )                MyDebugFunction( arguments )#else#define DebugWhatever( arguments ) ""#endif


my syntax is horribly off, but i don't use macros frequently enough... but you get the idea. In debug configuration your call to the debug function gets written in to the code, if not debug then it just gets changes to whitespace.

-me
Quote:Original post by Palidine
or just:

*** Source Snippet Removed ***

my syntax is horribly off, but i don't use macros frequently enough... but you get the idea. In debug configuration your call to the debug function gets written in to the code, if not debug then it just gets changes to whitespace.

-me


Close enough, just #define it to nothing:
#ifdef _DEBUG#define DebugWhatever( arguments ) \                 MyDebugFunction( arguments )#else#define DebugWhatever( arguments )#endif
I'd recommend using a macro to conditionally compile out the call.

#ifdef DEBUG && WIN32
#define WriteLog_(...) ::OutputDebugString(__VAR_ARGS__)
#else
#define WriteLog_(...)
#endif // DEBUG && WIN32

Variable argument macro's dont work on all compilers so there are other tricks you can do to support them if you need to make this work on multiple compilers.

Also, exception handling is a form of error management so having an empty catch in release doesn't sound like the best idea to me.
Graphics Programmer - Ready At Dawn Studios

This topic is closed to new replies.

Advertisement