Jen*_*ens 8 c macros language-lawyer
我有一个宏,WW用字宽为单位,比如
#define WW 64
Run Code Online (Sandbox Code Playgroud)
我要声明一个新的类型,foo_t作为对的别名uintX_t在<stdint.h>.我可以硬编码单词宽度和使用
#define uint(x) typedef uint ## x ## _t foo_t
uint(64);
Run Code Online (Sandbox Code Playgroud)
但是,uint(WW)显然是一个错误.我已经玩了一段时间使用宏#define expand(x) x,并以各种方式使用它,无济于事.我的最后一招是一个#if级联,如
#if WW == 64
typedef uint64_t foo_t;
#elif WW == 32
typedef uint32_t foo_t;
#elif WW == 16
typedef uint16_t foo_t;
#elif WW == 8
typedef uint8_t foo_t;
#else
#error "unsupported word width"
#endif
Run Code Online (Sandbox Code Playgroud)
我宁愿避免.有没有办法根据我的WW宏键入一个类型,以便uint(WW)最终扩展到uint64_t?我相信答案是"不",但是一些语言律师请证明我错了.
由于宏扩展的工作方式,您需要额外的间接级别:
#define maketype(x) uint ## x ## _t
#define uint(x) typedef maketype(x) foo_t
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
292 次 |
| 最近记录: |