为什么是-1> sizeof(int)? [英] Why is −1 > sizeof(int)?

查看:209
本文介绍了为什么是-1> sizeof(int)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

 模板< bool> class StaticAssert; 
模板<> class StaticAssert< true> {};
StaticAssert< (-1< sizeof(int))> xyz1; //编译错误
StaticAssert< (-1> sizeof(int))> xyz2; // OK

为什么是 -1> sizeof(int) true?


  1. -1 被提升为无符号(-1),然后无符号(-1) -1>
  2. sizeof(int)等效于 -1> size_t(4) if sizeof(int)is 4.如果这是为什么 -1> size_t(4)是false?

这个C ++标准是否兼容?

解决方案

以下是标准(ISO 14882)如何解释abort -1> sizeof(int)



关系运算符`>'在5.9(expr.rel / 2)中定义


是对算术操作数或
枚举类型执行的
。 ...


通常的算术转换定义在5(expr / 9)



...该模式称为通常的算术转换,定义如下:




  • 如果任一操作数是类型long
    double,...

  • 否则,如果任一操作数是dobule,...

  • ...

  • / ul>

    积分促销活动在4.5(conv.prom / 1)中定义


    char,signed char,
    unsigned char,short int或unsigned
    类型的右值可以转换为
    类型int的值,如果int可以
    表示所有源
    类型的值;否则,源右值可以
    转换为类型
    的右值。unsigned int。


    结果sizeof在5.3.3中定义(expr.sizeof / 6)


    结果是类型为
    的常量size_t < size_t是在C标准(ISO 9899)中定义的,它是无符号整数类型

    $ p>


    $ b < b
    $ b

    所以对于 -1> sizeof(int),>触发通常的算术转换。通常的算术转换将-1转换为unsigned int,因为int不能表示 size_t 的所有值。 -1 成为一个非常大的数量取决于平台。所以 -1> sizeof(int) true


    Consider the following code:

    template<bool> class StaticAssert;
    template<> class StaticAssert<true> {};
    StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error
    StaticAssert< (-1 > sizeof(int)) > xyz2; // OK
    

    Why is -1 > sizeof(int) true?

    1. Is it true that -1 is promoted to unsigned(-1) and then unsigned(-1) > sizeof(int).
    2. Is it true that -1 > sizeof(int) is equivalent to -1 > size_t(4) if sizeof(int) is 4. If this is so why -1 > size_t(4) is false?

    Is this C++ standard comformant?

    解决方案

    The following is how standard (ISO 14882) explains abort -1 > sizeof(int)

    Relational operator `>' is defined in 5.9 (expr.rel/2)

    The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. ...

    The usual arithmetic conversions is defined in 5 (expr/9)

    ... The pattern is called the usual arithmetic conversions, which are defined as following:

    • If either operand is of type long double, ...
    • Otherwise, if either operand is dobule, ...
    • Otherwise, if either operand is float, ...
    • Otherwise, the integral promotions shall be performed on both operands.
    • ...

    The integral promotions is defined in 4.5 (conv.prom/1)

    An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

    The result of sizeof is defined in 5.3.3 (expr.sizeof/6)

    The result is a constant of type size_t

    size_t is defined in C standard (ISO 9899), which is unsigned integer type.

    So for -1 > sizeof(int), the > triggers usual arithmetic conversions. The usual arithmetic conversion converts -1 to unsigned int because int cannot represent all the value of size_t. -1 becomes a very large number depend on platform. So -1 > sizeof(int) is true.

    这篇关于为什么是-1&gt; sizeof(int)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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