Visual C++ 2005 pre-processor bug?

Visual C++ 2005 pre-processor bug?

Post by SmlhbnBpbm » Sun, 18 Dec 2005 04:43:03


I preprocessed the following program using VC 2003 and it works fine:
(cl.exe /E /P hello.c)

#define AB\
C 7

int main () {

printf("Hi %d\n", ABC);
return 0;
}

But it fails with VC 2005:
hello.c(4) : error C2008: 'C' : unexpected in macro definition

Is this a bug? The VC version I used is:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for
80x86

Thanks!
Jianping
 
 
 

Visual C++ 2005 pre-processor bug?

Post by Victor Baz » Sun, 18 Dec 2005 04:56:32


If you are sure there is no space before the 'C' on the next line, then it
seems like a true bug in the preprocessor. The splicing of source lines
is done before decomposing into tokens and before executing preprocessor
directives. So, the behaviour should be to remove the '\' immediately
followed by a newline character thus converting

#define AB\<newline>C 7

into

#define ABC 7

(whatever whitespace precedes or follows this, is irrelevant)

V

 
 
 

Visual C++ 2005 pre-processor bug?

Post by QmVybmhhU0 » Sun, 18 Dec 2005 05:01:02

Splitting the name of the macro across multiple lines is generally a bad
idea. It's compiler dependant, but some compilers have been known to join the
lines inserting a space between the 2, while other compilers wont insert the
space.
To be safe, use either
#define \
ABC 7
or
#define ABC \
7

--
--BernhaSC
 
 
 

Visual C++ 2005 pre-processor bug?

Post by SmlhbnBpbm » Sun, 18 Dec 2005 05:11:03

> If you are sure there is no space before the 'C' on the next line, then it

Yes, I am sure there is no space. One weird thing is that if I did this:
cl.exe hello.c
It didn't complain and the program ran correctly with the output:
Hi 7

Jianping
 
 
 

Visual C++ 2005 pre-processor bug?

Post by Victor Baz » Sun, 18 Dec 2005 05:13:31


We've established that. VC++ v7.1 does it one way, VC++ v8 does it in
some other way. However, it does not mean that both are _allowed_. The
behaviour is very well defined by the Standard. See subclause 2.1,
"Phases of translation".

> but some compilers have been known to join the

Inserting a space is non-standard behaviour. If any particular compiler
does that, it's _non-compliant_.


That's called "a work around for this particular preprocessor bug".
Safety has nothing to do with it.

V
 
 
 

Visual C++ 2005 pre-processor bug?

Post by Victor Baz » Sun, 18 Dec 2005 06:17:59


I inadvertently broke my own promise and I apologise for that. Not only
was I replying to a top-posted message, I was replying to a message not
on the subject of the thread. Please disregard my reply and everything
derived from it. I'll be more careful from now on.