Archived

This topic is now archived and is closed to further replies.

How do I make a function compile to nothing in release mode?

This topic is 5379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There may be a simple answer to this that I am missing, but my problem is as follows: Can I make a member function compile to nothing in release mode like assert? I know assert can compile to nothing like this: #ifdef _DEBUG #define ASSERT(a, b) assert(a && b) #else #define ASSERT(a, b) #endif // _DEBUG Can I do something similar with a function to make it compile to nothing? All I can think of is this: void Class:Function(Param1, ...) { #ifdef _DEBUG // Do function stuff here #endif // _DEBUG } In release mode the function would do nothing, but would it compile to nothing or would it still get called? Does the compiler (MSVC 6) optimize out empty functions? Please help a dummy
pan narrans | My Website | Study + Hard Work + Loud Profanity = Good Code [edited by - pan narrans on March 24, 2003 2:59:02 PM]

Share this post


Link to post
Share on other sites
This is what the compiler sees the code like in debug mode:

void Class:Function(Param1, ...)
{

// Do function stuff here

}


and in !debug mode

void Class:Function(Param1, ...)
{
}


I don't know if it removes the function or not, most likely not. Do some debugging and you might find a solution... hmm... hard to put a break point in there though

Edit:

Just do like this to remove the function completely... or as much as possible maybe...


class Class
{
#ifdef _DEBUG
void Function();
#else
virtual void Function() {};
#endif
};

And just
#ifdef 
the function declaration. I don't really know what pure virtual functions are, but they might work here :/?



[edited by - Leffe on March 24, 2003 3:27:42 PM]

Share this post


Link to post
Share on other sites
No, the pure virtual has to do with inheritance and making interfaces and forcing derived classes to implement certain virtual functions.

In my experience, optimizing compilers are very good. It will probably (certainly?) optimize the function call out. I shouldnt'' worry about it, just #ifdef the function body.

An ancedote:
Once I wrote this math code...I was going to test it with something like..

vec3 res = vec3(10,3,4) + vec3(12,34,5);
cout << res.x << endl;

The compiler optimized that to (basically) cout << 22.
Uhhuh. MSVC7, though. I''m sure MSVC6 is good though. Don''t worry about it.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites

    
#ifdef _DEBUG

#define CallDebugFunction(x) x

#else

#define CallDebugFunction(x)

#endif


// In code

CallDebugFunction(Object.Function(1,2,3));


I don't know how you'd feel about that, it's much cleaner to just take out the body of the function like you've tried. But the above guarantees removal of the function call.

[edited by - cgoat on March 24, 2003 4:20:26 PM]

Share this post


Link to post
Share on other sites