# 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 on other sites
Comments are stripped before #defines are processed.

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

##### Share on other sites
The preprocessor understands, and therefore ignores, comments. FOO is defined as nothing.

##### Share on other sites
Quote:
 Original post by CodekaI 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

##### Share on other sites
Quote:
 Original post by StoryyellerIt was the best way I could think of for enabling and disabling many statements easily

Then you'll want to learn about the other preprocessor directives that are for exactly that purpose.

##### Share on other sites
Quote:
Original post by Storyyeller
Quote:
 Original post by CodekaI 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 on other sites
Quote:
 Original post by ChadwellWhy 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 on other sites
Quote:
 Original post by CodekaComments 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 on other sites
Quote:
Original post by scratt
Quote:
 Original post by CodekaComments 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 on other sites
Quote:
Original post by owl
Quote:
Original post by scratt
Quote:
 Original post by CodekaComments 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.

##### 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 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 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 on other sites
I personally tend to do this in C++:

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

or
static const bool debug = true; // or debug = 0, usually at class or function scopeif (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]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982193

• 9
• 24
• 10
• 9
• 13