为什么我不能在#if中使用sizeof()? [重复]

时间:2020-12-06 06:54:00

This question already has an answer here:

这个问题在这里已有答案:

I have this:

我有这个:

#if sizeof(int)
    #error Can't use sizeof in a #if
#endif

I get this compiler error:

我得到这个编译器错误:

missing binary operator before token "("

Why can't I use the sizeof operator here?

为什么我不能在这里使用sizeof运算符?

4 个解决方案

#1


12  

Because sizeof() is calculated after the preprocessor is run, so the information is not available for #if.

因为sizeof()是在预处理器运行后计算的,所以信息不适用于#if。

C compilers are logically split into two phases, even if most modern compilers don't separate them. First, the source is preprocessed. This involves working out and substituting all the preprocessor conditionals (#if, #define, replacing defined words with their replacements). The source is then passed, processed, to the compiler itself. The preprocessor is only minimally aware of the structure of C, it has no type knowledge, so it can't handle compiler-level constructs like sizeof().

C编译器在逻辑上分为两个阶段,即使大多数现代编译器没有将它们分开。首先,对源进行预处理。这包括计算并替换所有预处理器条件(#if,#define,用替换替换定义的单词)。然后将源传递,处理到编译器本身。预处理器只是最不了解C的结构,它没有类型知识,所以它不能处理像sizeof()这样的编译器级结构。

#2


2  

Because you can only use literal constants in a preprocessor directive. Besides, sizeof(int) is always larger than 0, so I believe this #if would be true all the time anyway.

因为您只能在预处理程序指令中使用文字常量。此外,sizeof(int)总是大于0,所以我相信这个#if无论如何都会是真的。

#3


-3  

Consider:

#if sizeof(MyClass) > 3
   #define MY_CONSTRAINT 2
#endif

class MyClass
{
   #if MY_CONSTRAINT == 3
      int myMember = 3;
   #endif
};

Now, this is prolly not written in the correct syntax as it's been a while since the last time I did C++, but the point still stands :)

现在,这是用正确的语法编写的,因为自从我上次使用C ++以来已经有一段时间了,但重点仍然是:)

#4


-3  

just use ordinary if-else

只需使用普通的if-else

if      (sizeof(x)==2)  {...}
else if (sizeof(x)==4)  {...}
else                    {...}

and compiler will optimize it in compile time...

和编译器将在编译时优化它...

#1


12  

Because sizeof() is calculated after the preprocessor is run, so the information is not available for #if.

因为sizeof()是在预处理器运行后计算的,所以信息不适用于#if。

C compilers are logically split into two phases, even if most modern compilers don't separate them. First, the source is preprocessed. This involves working out and substituting all the preprocessor conditionals (#if, #define, replacing defined words with their replacements). The source is then passed, processed, to the compiler itself. The preprocessor is only minimally aware of the structure of C, it has no type knowledge, so it can't handle compiler-level constructs like sizeof().

C编译器在逻辑上分为两个阶段,即使大多数现代编译器没有将它们分开。首先,对源进行预处理。这包括计算并替换所有预处理器条件(#if,#define,用替换替换定义的单词)。然后将源传递,处理到编译器本身。预处理器只是最不了解C的结构,它没有类型知识,所以它不能处理像sizeof()这样的编译器级结构。

#2


2  

Because you can only use literal constants in a preprocessor directive. Besides, sizeof(int) is always larger than 0, so I believe this #if would be true all the time anyway.

因为您只能在预处理程序指令中使用文字常量。此外,sizeof(int)总是大于0,所以我相信这个#if无论如何都会是真的。

#3


-3  

Consider:

#if sizeof(MyClass) > 3
   #define MY_CONSTRAINT 2
#endif

class MyClass
{
   #if MY_CONSTRAINT == 3
      int myMember = 3;
   #endif
};

Now, this is prolly not written in the correct syntax as it's been a while since the last time I did C++, but the point still stands :)

现在,这是用正确的语法编写的,因为自从我上次使用C ++以来已经有一段时间了,但重点仍然是:)

#4


-3  

just use ordinary if-else

只需使用普通的if-else

if      (sizeof(x)==2)  {...}
else if (sizeof(x)==4)  {...}
else                    {...}

and compiler will optimize it in compile time...

和编译器将在编译时优化它...