Using sizeof in the preprocessor

J

Johannes Schaub

I have this code in my pet project

#if sizeof -1 > 4
#error "This doesn't work on 64bit"
#endif

My friend was telling me that this shouldn't work because "sizeof is not
evaluated in the preprocessor". Yet the compiler accepts it and my program
works.

Can anybody please explain this? Is it a compiler bug? Thanks to all!
 
H

Huibert Bol

Johannes said:
I have this code in my pet project

#if sizeof -1 > 4
#error "This doesn't work on 64bit"
#endif

My friend was telling me that this shouldn't work because "sizeof is not
evaluated in the preprocessor". Yet the compiler accepts it and my program
works.

Identifiers in #if expressions are evaluated as zero, so the expression
is equivalent to

#if 0 -1 > 4

which is never true.
 
J

Juha Nieminen

Huibert Bol said:
Identifiers in #if expressions are evaluated as zero

It seems that at least with gcc "#if true" and "#if false" work as one
would expect. Is this standard?
 
H

Huibert Bol

Juha said:
It seems that at least with gcc "#if true" and "#if false" work as one
would expect. Is this standard?

Yes, "except true and false". Also the alternative tokens (and, andeq,
bitand, etc, ...) are replaced as they are not considered identifiers.
 
J

Johannes Schaub

Juha said:
It seems that at least with gcc "#if true" and "#if false" work as one
would expect. Is this standard?

There are explicit exceptions for true and false. The spec says

After all replacements due to macro expansion and the defined unary operator
have been performed, all remaining identiï¬ers and keywords, except for true
and false, are replaced with the pp-number 0, and then each preprocessing
token is converted into a token. The resulting tokens comprise the
controlling constant expression which is evaluated according to the rules of
5.19 ...
 
A

Alain Ketterlin

Juha Nieminen said:
It seems that at least with gcc "#if true" and "#if false" work as one
would expect. Is this standard?

Funny idea. Here (gcc/g++ 4.4.4), gcc and g++ both with -E give
different results on the following input:

#if true
#error "then"
#else
#error "else"
#endif

-- Alain.
 
J

Johannes Schaub

Johannes said:
I have this code in my pet project

#if sizeof -1 > 4
#error "This doesn't work on 64bit"
#endif

My friend was telling me that this shouldn't work because "sizeof is not
evaluated in the preprocessor". Yet the compiler accepts it and my program
works.

Can anybody please explain this? Is it a compiler bug? Thanks to all!

I want to emphasize that I do *not* intent to troll. I wanted to raise a
common issue and put people in alert mode when they see such code and don't
get a compiler error.

See http://chat.stackoverflow.com/transcript/message/752965#752965 and the
following rage of "FredNurk" for the discussion and why I need to defend
this question like that.
 
J

Juha Nieminen

Alain Ketterlin said:
Funny idea. Here (gcc/g++ 4.4.4), gcc and g++ both with -E give
different results on the following input:

#if true
#error "then"
#else
#error "else"
#endif

#if true
std::cout << "You rock!\n"
#else
#error "Your language is too old. Please upgrade."
#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top