commenting with define

Started by
11 comments, last by phresnel 15 years, 3 months ago
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]
Advertisement
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.
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]

This topic is closed to new replies.

Advertisement