如何定义类型取决于指针大小是否等于特定值的变量

How to define a variable whose type depends on whether the size of a pointer equals a specific value

我知道 C/C++ 预处理器不知道 sizeof,但我想以某种类似于此的方式定义变量:

#if sizeof(char*) == 8
uint64_t a;
#else
uint32_t a;
#endif

可能吗?

您可以这样使用 std::conditional_t

std::conditional_t<sizeof(char*) == 8, 
                   uint64_t, 
                   uint32_t> a;