开发者

Doubt in Conditional inclusion

This is actually extracted from my module (Pre-processor in C)

The conditional expression could contain any C operator except for the assignment operators,increment, and decrement operators.

I am not sure if I am getting this statement or not since I tried using this and it worked.Also for other manipulation a probable work around would be to simply declare macro or function inside the conditional expression,something like this开发者_如何学C to be precise.

Also I don't understand what is the rationale behind this rule. Could somebody explain?

Thanks


You seem to misunderstand what the phrase conditional expression refers to.
In this snippet

#if defined TEST
  int a = 0;
#endif

The conditional expression is the part being tested by the #if, meaning it is the defined TEST part.

The reason that assignment, increment and decrement are not allowed is because those operators want to change a variable, which is nonsensical in the context of the preprocessor.
The preprocessor works entirely based on textual substitution and evaluation of the resulting constant expressions.

If you have this code

#define X a++
#define Y 42

#if X == Y
#endif

Then in the test #if X == Y, first X and Y are replaced by their macro expansion (respectively a++ and 42), resulting in

#if a++ == 42

Next, a is replaced by its macro expansion. As there is no macro a, the replacement is defined to result in 0:

#if 0++ == 42

Now there are no possible macro names left to expand, so the preprocessor tries to evaluate the condition. As there is an attempt to increment the constant 0, this evaluation fails with an error.


The conditional expression could contain any C operator except for the assignment operators,increment, and decrement operators.

This says, you cannot write these,

#if X = 4 //wrong

#if X++  //this too wrong

#if X-- //this too

EDIT:

I just read your comment on Diego Sevilla's post,

Agreed but why something like this (ideone.com/ahXDA) is not allowed ? – Philando Gullible

This code actually equivalent to this which you write in the global scope,

//global code
int b=7; 
b++;  //ill-formed!

Which is not allowed in C (and C++). This however is allowed in C ONLY,

//legal in C Only
int b = 7;
int b; //well-formed!

Here the second definition is called tentative definition. C Standard says in $6.9.2/2,

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition.If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

Which again implies, the following is not allowed,

//illegal in both C and C++
int b=7;
int b=8; //error 

It's not a tentative definition anymore. It's redefinition, which is not allowed!


The paragraph you put in bold refers to #if expressions, so you can use operators:

#if VARIABLE > 3

etc., not the code that is inside the macros you define.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜