开发者

Is this a bug of gcc preprocessor?

#define BINARY_TREE_PARENT_CORRECT(son, parent) ((son) ? (son->parent == parent) : 1)

It turns out that the parent in son->parent which means a struct member will al开发者_C百科so be replaced by the parent in son, parent.

The gcc version is 4.1.2.

Do you think it's a bug or expected behavior?


The behavior is correct. All unquoted occurrences of parent are substituted. The preprocessor does not try to guess what you mean. It just replaces what you say.


This is expected behavior. The preprocessor does not know C's syntax (except when evaluating the controlling expression in an #if) -- it just replaces tokens.


Easy fix: Change the parameter name to be different from your element name.

#define BINARY_TREE_PARENT_CORRECT(son, par) ((son) ? (son->parent == par) : 1)

cpp won't match par against parent, so you'll get the behaviour you expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜