Visual-C ++和gcc之间的宏##连接运算符的差异 [英] Differences in Macro ## concatenation operator between Visual-C++ and gcc
问题描述
#define STRUCTMEMBER(())()成员,价值)GlobalStructInstance。 ## Member =价值
...
STRUCTMEMBER(Item,1);
这在Visual C ++中完美工作,但gcc 3.4.5(MingGW)产生以下错误: / p>
粘贴。和Item不会给出有效的预处理令牌
当我使用 - >运算符时,也会发生这种情况。我没有发现关于连接的提示,这些操作符的使用是被禁止的。
有人有想法吗?
也许Visual C ++将两个空格粘贴在一起以创建另一个空间。不是空格是代币,但它可以让你的代码工作。
object.member
不是一个令牌,它是三个令牌,所以你不需要使用令牌粘贴来实现你描述的宏。只需删除'##',它就可以在任何地方工作。
I'm having a macro like this ( not exactly, but function is quite equivalent):
#define STRUCTMEMBER(Member,Value) GlobalStructInstance. ## Member = Value
...
STRUCTMEMBER(Item,1);
This works perfectly in Visual C++, but gcc 3.4.5 (MingGW) yield the following error:
pasting "." and "Item" does not give a valid preprocessing token
This also happens when I use the "->" operator. I didn't found hints on concatenation, that the usage of these operators is forbidden.
Does anyone have an idea ?
Maybe Visual C++ is pasting a couple of spaces together to make another space. Not that whitespaces are tokens, but it would allow your code to work.
object.member
is not a token, it's three tokens, so you don't need token-pasting to implement the macro you describe. Just remove the '##' and it should work everywhere.
[Edit: just checked, and the result of using ## to form something that isn't a valid token is undefined. So GCC is allowed to reject it and MSVC is allowed to ignore it and perform no paste, as far as I can tell.]
这篇关于Visual-C ++和gcc之间的宏##连接运算符的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!