简体   繁体   English

结合两个宏?

[英]Combine two Macros?

Following macro MY_SIZE_OF_VARIABLE gives sizeof variables and MY_SIZE_OF_DATATYPE gives size of inbuilt datatype. 紧随其后的宏MY_SIZE_OF_VARIABLE给出sizeof变量,而MY_SIZE_OF_DATATYPE给出内建数据类型的大小。 I want to combine these two macro in one Macro. 我想将这两个宏合并为一个宏。

#define MY_SIZE_OF_VARIABLE(X) (char *)(&X+1)-(char *)(&X)
#define MY_SIZE_OF_DATATYPE(X) (X*)0 + 1

I tried to combine these but i can't do that. 我试图将这些结合起来,但是我做不到。

You can't do that. 你不能那样做。 Part of the evil magic that is preprocessor macros is that it is a simple text replacement with no judgment of syntactic context. 预处理器宏的邪恶之处在于,它是一种简单的文本替换,无需判断语法上下文。 This is so that you can do all sorts of wacky stuff like concatenating symbols, lazy evaluations, etc. etc. The other end of that is that you can't turn around and ask it what its parameter is - it's all just text in a macro. 这样一来,您就可以进行各种古怪的工作,例如连接符号,惰性求值等。另一端是您无法掉头问问它的参数是什么-它全都是文本。宏。

Short of declaring a macro for every symbol in your source , including both types and variables, that consists of the symbol concatenated with __MY_SIZE or something, you really can't do this. 如果没有为源中的每个符号 (包括类型和变量)声明一个宏,该宏都由与__MY_SIZE或其他内容串联在一起的符号组成,则您实际上无法做到这一点。 You are stuck with a sizeof you didn't write, for better or worse. 无论是好是坏,您都被卡住了没有写的sizeof

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM