views:

127

answers:

5

A few questions about the C++ preprocessor:

  1. how to make the preprocessor go to a new line into the preprocessoring code?
  2. how to make the preprocessor insert a tab character or multiple spaces into the preprocessoring code?
  3. how to make the preprocessor insert comments into the preprocessoring code?
+5  A: 

Regarding #3, it's the preprocessor's responsibility to remove comments from the code, I don't think it's allowed to leave them in. In any case this would be a flag specific to the C++ compiler you're using so you should specify your environment.

Motti
+2  A: 

how to make the preprocessor go to a new line into the preprocessoring code?

Why?

how to make the preprocessor insert a tab character or multiple spaces into the preprocessoring code?

Why?

how to make the preprocessor insert comments into the preprocessoring code?

Why?

The preprocessor is a pre processor, it runs before the code is converted into machine code. The whitespace and comments you want to add will have no effect on output of the application.

If you're trying to control the output of gcc -E, or something similar then you're barking up the wrong tree.

taspeotis
This isn't an answer. It belongs as a comment.
PigBen
@PigBen - Why? Why? Why?
taspeotis
+2  A: 

You are doing it wrong... PREPROCESSOR is not for that purpose.

Rin
Not for that purpose is GRAMMAR
taspeotis
Tell it to master Yoda...
Rin
+4  A: 

Questions 2) and 3) don't make much sense, as other people have outlined.

As for question 1, I assume what you mean is multi-line macros, which can be done in this way:

#define FOO line 1 \
    line 2  \
    line 3  \
    ...     \
    line n

Note the missing \ at the last line!

Tim Čas
+1  A: 

1) use the backslash, as Tim pointed out

2) I don't think you can

3)

#define COMMENT /##/ this is a comment
#define CPPCOMMENT(c) /##/ c
#define CCOMMENT(c) /##* c *##/

COMMENT
CPPCOMMENT(This is a c++ comment)
CCOMMENT(This is a c comment)

Edit

2 Caveats

1) Doesn't work in all compilers.

2) Don't do this, it's stupid.

PigBen
Your macros don't work in GCC 4.1.1. And, i imagine, in a number of other environments.
cHao
In VS 2010, they only semi work -- the macros actually become comments, but they get stripped out by the preprocessor. So nothing's actually inserted, which defeats the purpose of this whole evil mess. But at least the macros don't cause errors there.
cHao
@cHao The fact that it doesn't work in GCC is GCC's problem. There's no reason defined in the standard that says it shouldn't. It does work in VS 2010 if you set the keep comments flag, it just results in a source file that's uncompilable because it still has comments in it after preprocessing. But if you take the output of the preprocessor, then compile it again, it works. I don't see a purpose to this whole evil mess in the first place, so I don't know how that purpose could be defeated.
PigBen
PigBen, it also fails with [Comeau's online compiler](http://www.comeaucomputing.com/tryitout/), which makes me wary. In a decade, I've found one bug in Comeau, but several dozens in VC.
sbi
@PigBen: There's no reason defined in the standard why it *should*. And even if you managed to find one, at least two popular compilers choke on it, so standard or not, it's useless.
cHao
@cHao: I completely agree with you, there is no reason why it should. In fact, I can think of very good reasons why it shouldn't. And I guarantee that it's useless. I'm just answering the question. If there is a way to do it, even if it only works on one compiler, what's wrong with me providing that method?
PigBen
@PigBen: What's wrong with it is that (1) you presented it as "this is how you do it", though it's not guaranteed to work at all (and actually breaks in some allegedly-standard compilers), and (2) it encourages people who want to do this kind of thing to actually do it (using non-portable, almost-certainly-nonstandard syntax), when what's really needed is a rethinking of the problem. Oh, and (3): Even if the syntax worked, the work would be undone by the preprocessor anyway, as rightly it should be -- so any problem that would have prompted the question still hasn't been solved.
cHao
@cHao: See update. That better?
PigBen