Preprocessing directives

R

Ron

Please, consider the following code:

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?

But how can be possible? Syntax analysis, following the standard, is done
after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?

Regards.
 
R

Rolf Magnus

Ron said:
Please, consider the following code:

#include <iostream>
using std::cout;
using std::endl;
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?

I doubt that.
But how can be possible? Syntax analysis, following the standard, is
done after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?

It can't. That's why I doubt it. But I'm not a language guru.
 
J

Jack Klein

Please, consider the following code:

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?

Any compiler that outputs 200 is broken. It is not a standard
feature, it is a non-conforming "feature".
But how can be possible? Syntax analysis, following the standard, is done
after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?

Regards.

There are almost an infinite number of ways one could write a compiler
to create this defect. If you want to know which one occurred in this
particular compiler, you need to either examine the source code if
available, or ask the people who wrote it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top