Ror*_*raΖ 6 c c++ macros types c-preprocessor
我搜索了SO,但没有找到这个具体问题的答案.请原谅我已经回答了.
如果您有以下内容:
#define MACRO 40
Run Code Online (Sandbox Code Playgroud)
您不将它分配给在循环中使用它的变量:
for(int i = 0; i < MACRO; i++) {...
Run Code Online (Sandbox Code Playgroud)
然后,每个处理器创建:
for(int i = 0; i < 40; i++) {...
Run Code Online (Sandbox Code Playgroud)
然后编译器会将它隐式地转换为int,因为比较是使用类型int i吗?我看过这个问题#define变量的类型,而Edgar Bonet的一些答案暗示了编译器选择如何处理宏的顺序?
这个问题,C++如何隐式地将参数转换为比较器,如<?,也有人建议,但只描述了隐式转换如何与两种类型进行比较.由于宏实际上没有类型,我不确定这是否适用.
该预处理器扩展宏编译器甚至看到任何东西之前.我们可以看到预处理数字没有类型,可以通过草案C99标准部分6.4.8 预处理数字说明:
预处理号码没有类型或值; 它在成功转换(作为转换阶段7的一部分)之后获取浮动常量令牌或整数常量令牌.
C++标准草案中的相同部分是2.10.
正如我们在C预处理器Wikipedia中看到的那样,宏扩展发生在第4阶段.
C语言术语中的整数常量转换和C++术语中的整数文字的转换在C99标准部分的6.4.4.1 整数常量和第5段中的下表中说明:
整数常量的类型是相应列表中可以表示其值的第一个
Octal or Hexadecimal
Suffix Decimal Constant Constant
---------------------------------------------------------------------------
none int int
long int unsigned int
long long int long int
unsigned long int
long long int
unsigned long long int
---------------------------------------------------------------------------
u or U unsigned int unsigned int
unsigned long int unsigned long int
unsigned long long int unsigned long long int
---------------------------------------------------------------------------
l or L long int long int
long long int unsigned long int
long long int
unsigned long long int
---------------------------------------------------------------------------
Both u or U unsigned long int unsigned long int
and l or L unsigned long long int unsigned long long int
---------------------------------------------------------------------------
ll or LL long long int long long int
unsigend long long int
---------------------------------------------------------------------------
Both u or U unsigned long long int unsigned long long int
and ll or LL
---------------------------------------------------------------------------
表是此答案的修改版本.C++标准草案中涉及此部分的部分2.14.2也是一个类似的表.
因此,在您的示例40中没有后缀并且是十进制常量,并且可以从表的该部分表示的第一个类型是int.
在这一点上,我们现在使用的效果最终40与<运营商.由于i和40都是算术类型,因此将执行通常的算术转换,在这种情况下仍然是int.对于C99,这将在章节6.3.1.8和C++第5节中介绍.
| 归档时间: |
|
| 查看次数: |
2936 次 |
| 最近记录: |