Anyway to have a macro generate comments?

Started by
23 comments, last by Etnu 19 years, 10 months ago
quote:Original post by Extrarius
You could always do:
#ifdef _DEBUG#define DebugBlock(x) {x}#else#define DebugBlock(x)#endifDebugBlock(     DebugOnlyFunc();     OtherDebugOnlyFunc();     AnotherDebugOnlyFunc();     OneMoreDebugOnlyFunc();     NextDebugOnlyFunc();)
Be very careful with such macros.

DebugBlock(
std::map<string, int> word_frequencies; // ouch
)

I recommend that the OP use assert() or something similar, or just use #ifdef and stop whining.
Advertisement
Why not just do
if(_DEBUG){    DoFirstThing();    DoSecondThing();    DoSomethingElse();}
It will definitely be optimized out in release builds and it has the added advantage that the compiler will always check the syntax of your debug code even when doing a release build which you don't get with #ifdef or with your comment idea if that could actually be made to work (it can't). I don't see the point of defining an extra macro on top of this when you can just write the if out directly - it doesn't take any more typing.

[edited by - mattnewport on June 6, 2004 5:08:11 AM]

Game Programming Blog: www.mattnewport.com/blog

quote:Original post by Etnu
quote:
Or #ifdef. I mean, that''s why it''s there. Leave comments for, well, comments.


That was the original point of this thread - #ifdef is extremely ugly, and I was trying to get rid of it.


I don''t find it extremely ugly. The preprocessor can be ugly (both code-wise and aesthetically) but this is one place I think it''s all right. And even if it were ugly in this case, #ifdef''s are probably the most common way to do code removal like this and are therefore one of the most readable. If it''s just for you and not intended for anyone else, I think your best bet is if(0);else/if(1);else. But you seem to have worries that that won''t be optimized out. There''s only one way to be absolutely certain it will be removed in all cases, and that''s an #ifdef. So, options:

1) Use #ifdef, be sure it''s removed, and follow a common idiom
2) Use one of the macros provided, be sure it''s removed on any real-world optimizing compiler, and have something that you find more visually pleasing
3) Change languages to one that has what you want built in. C and C++ aren''t the only, nor in general the best, language.
I thought _DEBUG was just defined with no value? wouldn''t if(_DEBUG) result in a compiler error? That''s probably the best option, though, so I think I''ll go that route.

Way walker: I''m certainly not going to switch languages just because of something like this. It''s mostly got to do with the auto formatting and such that''s gotten to me lately (MSVC automatically left-aligns #defines, and as near as I can tell there''s no way to control it). It''s a rather minor annoyance, and I was just trying to see if anyone else had come up with a more robust way to go about it.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

quote:Original post by Etnu
I thought _DEBUG was just defined with no value? wouldn''t if(_DEBUG) result in a compiler error? That''s probably the best option, though, so I think I''ll go that route.


Shows my ignorance I thought you were defining _DEBUG, in which case you could just give it a value.

quote:
Way walker: I''m certainly not going to switch languages just because of something like this. It''s mostly got to do with the auto formatting and such that''s gotten to me lately (MSVC automatically left-aligns #defines, and as near as I can tell there''s no way to control it). It''s a rather minor annoyance, and I was just trying to see if anyone else had come up with a more robust way to go about it.


Well, I don''t think preprocessor directives allow (at least, as defined by the standard they don''t allow) whitespace before the # nor immediately after the #, so it may be trying to make your code portable. I can understand this concern, that''s why I comment like crazy when using #ifdef''s, etc.

And there are other reasons you may want to switch languages but that''s another thread, eh?

This topic is closed to new replies.

Advertisement