Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #5258 > unrolled thread
| Started by | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| First post | 2011-05-21 15:35 +0200 |
| Last post | 2011-05-21 22:19 +0200 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.c++
Using sizeof in the preprocessor Johannes Schaub <schaub.johannes@googlemail.com> - 2011-05-21 15:35 +0200
Re: Using sizeof in the preprocessor Pete Becker <pete@versatilecoding.com> - 2011-05-21 09:42 -0400
Re: Using sizeof in the preprocessor Huibert Bol <huibert.bol@quicknet.nl> - 2011-05-21 16:08 +0200
Re: Using sizeof in the preprocessor Pete Becker <pete@versatilecoding.com> - 2011-05-21 10:37 -0400
Re: Using sizeof in the preprocessor Juha Nieminen <nospam@thanks.invalid> - 2011-05-21 18:51 +0000
Re: Using sizeof in the preprocessor Huibert Bol <huibert.bol@quicknet.nl> - 2011-05-21 21:52 +0200
Re: Using sizeof in the preprocessor Johannes Schaub <schaub.johannes@googlemail.com> - 2011-05-21 21:54 +0200
Re: Using sizeof in the preprocessor Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-05-21 21:54 +0200
Re: Using sizeof in the preprocessor Juha Nieminen <nospam@thanks.invalid> - 2011-05-22 06:02 +0000
Re: Using sizeof in the preprocessor Johannes Schaub <schaub.johannes@googlemail.com> - 2011-05-21 22:19 +0200
| From | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2011-05-21 15:35 +0200 |
| Subject | Using sizeof in the preprocessor |
| Message-ID | <4dd7bfa5$0$7624$9b4e6d93@newsspool1.arcor-online.net> |
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!
[toc] | [next] | [standalone]
| From | Pete Becker <pete@versatilecoding.com> |
|---|---|
| Date | 2011-05-21 09:42 -0400 |
| Message-ID | <2011052109420832334-pete@versatilecodingcom> |
| In reply to | #5258 |
On 2011-05-21 09:35:53 -0400, Johannes Schaub 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! Bug or extension. The preprocessor does text manipulation; it doesn't know anything about types (although it has its own set of rules for arithmetic). -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)
[toc] | [prev] | [next] | [standalone]
| From | Huibert Bol <huibert.bol@quicknet.nl> |
|---|---|
| Date | 2011-05-21 16:08 +0200 |
| Message-ID | <ir8h1q$f2o$1@dont-email.me> |
| In reply to | #5258 |
Johannes Schaub wrote: > 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. -- Huibert "Okay... really not something I needed to see." --Raven
[toc] | [prev] | [next] | [standalone]
| From | Pete Becker <pete@versatilecoding.com> |
|---|---|
| Date | 2011-05-21 10:37 -0400 |
| Message-ID | <2011052110370061214-pete@versatilecodingcom> |
| In reply to | #5260 |
On 2011-05-21 10:08:58 -0400, Huibert Bol said: > Johannes Schaub wrote: > >> 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, Good point. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-05-21 18:51 +0000 |
| Message-ID | <4dd809a7$0$2844$7b1e8fa0@news.nbl.fi> |
| In reply to | #5260 |
Huibert Bol <huibert.bol@quicknet.nl> wrote: > 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?
[toc] | [prev] | [next] | [standalone]
| From | Huibert Bol <huibert.bol@quicknet.nl> |
|---|---|
| Date | 2011-05-21 21:52 +0200 |
| Message-ID | <ir9562$br4$1@dont-email.me> |
| In reply to | #5285 |
Juha Nieminen wrote: > Huibert Bol <huibert.bol@quicknet.nl> wrote: >> 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? Yes, "except true and false". Also the alternative tokens (and, andeq, bitand, etc, ...) are replaced as they are not considered identifiers. -- Huibert "Okay... really not something I needed to see." --Raven
[toc] | [prev] | [next] | [standalone]
| From | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2011-05-21 21:54 +0200 |
| Message-ID | <4dd81857$0$6541$9b4e6d93@newsspool4.arcor-online.net> |
| In reply to | #5285 |
Juha Nieminen wrote: > Huibert Bol <huibert.bol@quicknet.nl> wrote: >> 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? 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 ...
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2011-05-21 21:54 +0200 |
| Message-ID | <87zkmfde2c.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #5285 |
Juha Nieminen <nospam@thanks.invalid> writes: > Huibert Bol <huibert.bol@quicknet.nl> wrote: >> 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? 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.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-05-22 06:02 +0000 |
| Message-ID | <4dd8a6da$0$2871$7b1e8fa0@news.nbl.fi> |
| In reply to | #5292 |
Alain Ketterlin <alain@dpt-info.u-strasbg.fr> wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2011-05-21 22:19 +0200 |
| Message-ID | <4dd81e48$0$6557$9b4e6d93@newsspool4.arcor-online.net> |
| In reply to | #5258 |
Johannes Schaub wrote: > 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web