是C宏隐式转换? [英] Are C macros implicitly cast?

查看:184
本文介绍了是C宏隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查SO,但还没有找到一个答案的具体问题。原谅我,如果它已经有了答案。

如果您具备以下条件:

 的#define MACRO 40

您不要把它分配给一个变​​量,你在一个循环中使用它:

 的for(int i = 0; I<万家乐,我++){...

该perprocessor然后创建:

 的for(int i = 0; I< 40;我++){...

请问编译器则含蓄地将其转换为int,因为比较有型 INT I ?我看的#define 的变量的这个问题,类型,也有不少的答案下来埃德加博内特意味着有在该编译器选择如何看待宏观订单?

这个问题,<一个href=\"http://stackoverflow.com/questions/14603882/how-does-c-implicitly-cast-arguments-to-a-comparator-such-as\">How确实的C ++隐式转换参数给比较器例如&lt ;? 时,也有人建议,但仅描述了铸造如何隐与两种类型的比较工作。由于宏并没有真正有型,我不知道这是否适用。


解决方案

借助 preprocessor ​​扩展宏编译器甚至看到任何东西之前。我们可以看到,preprocessing号码不要将有一个类型的草案C99标准部分 6.4.8 preprocessing号的它说:


  

一个preprocessing数量没有类型或价值;它获得两个
  转换成功后(如转换阶段7的一部分)到
  浮点常量令牌或整型常量令牌。


在C草案同一节++标准是 2.10

正如我们在çpreprocessor维基百科的文章宏扩展见发生在第4阶段。

在C语言术语和整型常量整型常量在C ++术语的转换涵盖了C99标准草案 6.4.4.1 整型常量的并在段落的 5 的它说见下表:


  

类型的整型常量是第一个对应列表
  在其值可以被重新presented



                                           八进制或十六进制
后缀十进制常数常数
-------------------------------------------------- -------------------------
没有INT INT
              长整型无符号整型
              久长整型长整型
                                            unsigned long int类型
                                            得到long long int
                                            无符号长长整型
-------------------------------------------------- -------------------------
ü或U无符号整型无符号整型
              unsigned long int类型unsigned long int类型
              无符号长长整型无符号长长整型
-------------------------------------------------- -------------------------
I或L长整型长整型
              得到long long int unsigned long int类型
                                            得到long long int
                                            无符号长长整型
-------------------------------------------------- -------------------------
无论ü或U unsigned long int类型unsigned long int类型
和L或L无符号长长整型无符号长长整型
-------------------------------------------------- -------------------------
LL或LL得到long long int得到long long int
                                            unsigend得到long long int
-------------------------------------------------- -------------------------
无论ü或U unsigned long类型长整型无符号长长整型
和LL或LL
-------------------------------------------------- -------------------------

表是从该回答的所述一个的修改版本。覆盖这在C草案的部分++标准是一款 2.14.2 其中也有类似的表。

因此​​,在您的例子 40 没有后缀,是一个十进制常量和第一类它可以重新从表中该节psented $ P $是 INT

在这一点上,我们现在使用的效果 40 &LT结束; 运营商。由于 I 40 算术类型的话通常的算术转换将被执行,在这种情况下仍然会<青霉> INT 的。对于 C99 的这部分内容在第 6.3.1.8 和C ++部分的 5

I've searched SO, but haven't found an answer to this specific questions. Forgive me if it's already been answered.

If you have the following:

#define MACRO  40

You don't assign it to a variable you use it in a loop:

for(int i = 0; i < MACRO; i++) {...

The perprocessor then creates:

for(int i = 0; i < 40; i++) {...

Would the compiler then implicitly cast it to an int since the comparison is with type int i? I've looked at this question Type of #define variables, and quite a few answers down Edgar Bonet implies that there is an order in which the compiler chooses how to treat the macro?

This question, How does C++ implicitly cast arguments to a comparator such as <?, was also suggested, but only describes how implicit casting works with a comparison with two types. Since a macro doesn't really have a type I'm not sure if this applies.

解决方案

The preprocessor expands macros before the compiler even sees anything. We can see that preprocessing numbers don't have a type by going to the draft C99 standard section 6.4.8 Preprocessing numbers which says:

A preprocessing number does not have type or a value; it acquires both after a successful conversion (as part of translation phase 7) to a floating constant token or an integer constant token.

The same section in the draft C++ standard is 2.10.

As we can see in C preprocessor Wikipedia article macro expansion happens in phase 4.

The conversion of integer constants in C terminology and integer literals in C++ terminology is covered in the draft C99 standard section 6.4.4.1 Integer constants and the following table in paragraph 5 which says:

The type of an integer constant is the first of the corresponding list in which its value can be represented

                                           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
---------------------------------------------------------------------------

Table is a modified version of the one from this answer. The section that covers this in the draft C++ standard is section 2.14.2 which also has a similar table.

So in your example 40 has no suffix and is a decimal constant and the first type it can be represented from that section of the table is int.

At this point we now end up with the effects of using 40 with the < operator. Since i and 40 are both arithmetic types then the usual arithmetic conversions will be performed, which in this case will still be int. For C99 this is covered in section 6.3.1.8 and C++ section 5.

这篇关于是C宏隐式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆