Topic: Preprocessing directives


Author: jack.adam@gmx.net (Jack Adam)
Date: Thu, 13 Nov 2003 03:35:45 +0000 (UTC)
Raw View

Ron wrote:
> Please, consider the following code:
>
> #include <iostream>
> using namespace std;
>
> const int a = 1;
> #if a == 1
> #define VAR 200
> #else
> #define VAR 100
> #endif
>
> int main() {
>    cout << VAR << endl;
>    return 0;
> }
>
> Under some compiler it prints 200. So a preprocessing directive can check
> constant values. Is it a standard C++ feature?

A processing directive like that can check an integral constant
expression, but prior to evaluation the identifiers that are macros are
replaced with their expansion and the other identifiers are replaced
with 0.  There are also some peculiarities when evaluating the expression.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com (Richard Smith)
Date: Thu, 13 Nov 2003 03:35:45 +0000 (UTC)
Raw View
Ron wrote:

> #include <iostream>
> using namespace std;
>
> const int a = 1;
> #if a == 1
> #define VAR 200
> #else
> #define VAR 100
> #endif
>
> int main() {
>    cout << VAR << endl;
>    return 0;
> }
>
> Under some compiler it prints 200.

There seems to me to be no doubt that this is a perfectly
legal program that should print 100.  The processor knows
nothing about the variable a -- the definition will not have
been parsed by the time the #if directive is evaluated.
When the preprocessor sees an unknown identifier, it will
replace it with the number 0 [see 16.1/4].  Thus the
conditional of the #if directive reads 0 == 1, which is
clearly false.  Thus, VAR is defined to 100 and that is what
the program should output.

I'm intrigued that you have found a compiler under which it
prints 200.  Which compiler is this?

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: Fri, 14 Nov 2003 02:36:26 +0000 (UTC)
Raw View
Ron wrote:
> Please, consider the following code:
>
> #include <iostream>
> using namespace std;
>
> const int a = 1;
> #if a == 1
> #define VAR 200
> #else
> #define VAR 100
> #endif
>
> int main() {
>    cout << VAR << endl;
>    return 0;
> }
>
> Under some compiler it prints 200.

That compiler does nto conform to the rules about phases of translation
in section 2.1 of the C++ standard.

In the line
 #if a == 1
the 'a' can refer only to a name defined in a #define directive, not to
the 'a' in the const int declaration. If no such 'a' has been #define'd,
this 'a' has the value 0.

(I'm assuming the <iostream> header does not #define a macro 'a', of
course.)

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]