Sign in to follow this  
Storyyeller

commenting with define

Recommended Posts

Hi, everyone, I started teaching myself c++ a couple weeks ago. Today I had a question I figured I might as well try posting here. I was under the impression that when you use #define, it blindly replaces all occurrences before compiling beings, so I did
#define FOO //
FOO x++;

It compiled fine, but for some reason, the statements executed, even though they were supposed to be commented out. What did I do wrong? I hope this isn't a stupid question.

Share this post


Link to post
Share on other sites
Quote:
Original post by Storyyeller
Quote:
Original post by Codeka

I hope you weren't planning to do that for real, though....


It was the best way I could think of for enabling and disabling many statements easily


Why not just use // in front or the line? It's less characters than FOO anyway.
Or for a longer statement, just use /* at the start and */ at the end of what you want to comment out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chadwell
Why not just use // in front or the line? It's less characters than FOO anyway.
Because it wouldn't allow him to conditionally disable a large number of nonconsecutive lines.

BTW, Storyyeller, there's a hackish and ugly way of doing this:

#define FOO if(0)

But yeah, #ifndef is your friend here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Comments are stripped before #defines are processed.


Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.

Share this post


Link to post
Share on other sites
Quote:
Original post by scratt
Quote:
Original post by Codeka
Comments are stripped before #defines are processed.


Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.


lol, if you comment the defines, then they will get stripped thus will not be compiled.

pass me some of those beers, tnx. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by owl
Quote:
Original post by scratt
Quote:
Original post by Codeka
Comments are stripped before #defines are processed.


Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.


lol, if you comment the defines, then they will get stripped thus will not be compiled.

pass me some of those beers, tnx. ;)


lol
I guess i read it the wrong way around.
i.e. Misinterpreting comments are stripped, as meaning // is stripped, before #define is processed, thus exposing #define.
Which it obviously does not.

My bad. :)

Share this post


Link to post
Share on other sites
Funnily enough, I was wanting to know whether this was possible myself, just last night. (Not for any reason, just out of curiosity)

I had found this article, which can help you:
Comments inside macroes (Followup article)

I recall having another article also, that explained another neat comment-macro trick, but I guess I forgot to bookmark it. (doh)

[Edited by - Servant of the Lord on January 18, 2009 11:34:56 PM]

Share this post


Link to post
Share on other sites
However, also read the comment to that article: it seems to say the trick only works in MS compilers since by the time macros are expanded comments have already been handled and concatenating // just produces an invalid preprocessing token.

Share this post


Link to post
Share on other sites
I personally tend to do this in C++:

enum { debug = 1 }; // or debug = 0, usually at class or function scope
if (debug) ::std::cout << "Fubar." << ::std::endl;

or
static const bool debug = true; // or debug = 0, usually at class or function scope
if (debug) ::std::cout << "Fubar." << ::std::endl;

. I prefer the first one, as it comes at no extra storage and I can not (for whatever drunken reason) accidentally const_cast<>() it. If 'debug' is considered compile time constant (which is guaranteed for enum-values), the if-statement will be optimised away be every serious compiler.

Of course macro comments would be funky, as it would allow for FORTRAN-like comments:
#define C //
C ye olde FORTRAN comment, dude!
C heh.




sidenote:
After all, don't forget that the C-preprocessor (cpp) is for C, hence it is streamlined towards exactly that programming language. Some people use the cpp as a general text preprocessor, but which is simply wrong and there are other, general purpose preprocessors.

[Edited by - phresnel on January 19, 2009 7:38:55 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this