简体   繁体   English

C ++如何隐式地将参数转换为比较器,如<?

[英]How does C++ implicitly cast arguments to a comparator such as <?

I had thought that this would be an easy question resolve via Google, but I can't seem to find a definitive (or even speculative) answer: 我原以为这可以通过谷歌轻松解决问题,但我似乎无法找到明确的(甚至是推测性的)答案:

When using a comparator statement, in which order does implicit casting occur? 使用比较器语句时,隐式转换的顺序是什么?

int i = -1;
size_t t = 1;

bool result = i < t;

Is this equivalent to: 这相当于:

bool result = i < int(t);    // equals true

or: 要么:

bool result = size_t(i) < t;    // equals false

That is the easy part of the question - the second part is "what is the general rule", as it could be: 这是问题的简单部分 - 第二部分是“一般规则是什么”,因为它可能是:

  1. The 'simpler' argument is always converted into the 'more complex' argument (ie size_t->int), or 'simpler'参数总是转换为'更复杂'的参数(即size_t-> int),或
  2. The first (or second) argument is always converted to the type of the second (or first) argument, or 第一个(或第二个)参数始终转换为第二个(或第一个)参数的类型,或
  3. The inbuilt primitives such as size_t and ints have specific comparator operators which specify the casting case-by-case. 内置的原语(如size_t和ints)具有特定的比较运算符,用于逐个指定转换。

All three seem reasonable, although the second would yield significantly different behaviour to what most people would intuitively expect. 所有三个看起来都是合理的,尽管第二个会产生与大多数人直观期望的行为明显不同的行为。

The VC++ compiler seems to think it's worth a level 3 warning when you compare an int with a size_t - and yet it only gives a level 4 warning when you return a negative number from a function that returns a size_t (which results in a number just over half the maximum integer being returned). 当你将int与size_t进行比较时,VC ++编译器似乎认为它值得一个3级警告 - 然而当你从返回size_t的函数返回负数时它只给出4级警告(这导致一个数字只是超过返回的最大整数的一半)。

In an effort to get rid of all level 4 warnings, I now explicitly cast everything anyway, but I wanted to know "the truth". 为了摆脱所有4级警告,我现在无论如何都明确地投了一切,但我想知道“真相”。 This must be defined somewhere... 这必须在某处定义......

The rules are fairly complex, and depend on the implementation. 规则相当复杂,取决于实现。 Basically, however: 但基本上:

  1. Both types are "promoted". 两种类型都被“提升”。 This means that anything smaller than int is promoted to int . 这意味着任何小于int东西都会被提升为int (In the unlikely case that size_t is smaller than int , it will be promoted to a signed int , and loose its unsignedness.) (在不太可能的情况下, size_t小于int ,它将被提升为signed int ,并且松散其无符号。)

  2. If one of the types can contain all of the values of the other, the other is converted to this type. 如果其中一个类型可以包含另一个类型的所有值,则另一个类型将转换为此类型。

  3. If one of the types is unsigned, and the other signed, and they have the same size, the signed is converted to the unsigned. 如果其中一个类型是无符号的,另一个是有符号的,并且它们具有相同的大小,则signed将转换为unsigned。

For int and size_t (which is required to be unsigned), this means that unless size_t is smaller than int , the int will be converted to a size_t . 对于intsize_t (需要无符号),这意味着除非size_t小于int ,否则int将转换为size_t

$18.2/6- The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object. $ 18.2 / 6-类型size_t是一个实现定义的无符号整数类型,它足够大,可以包含任何对象的字节大小。

$5.9 - Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type. $ 5.9 - 否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩, 则带有符号整数类型的操作数应转换为具有无符号整数类型的操作数的类型。 For the first part, bool is promoted to an int, before the conversion as specified by the C++ standard. 对于第一部分,bool在C ++标准指定的转换之前被提升为int。

So, this means that 'i' is converted to the type of 'size_t' (assuming size_t is of type 'unsigned int' or greater). 因此,这意味着'i'被转换为'size_t'的类型(假设size_t的类型为'unsigned int'或更高)。 Then the results which is of type 'unsigned int' is converted to 'bool' (which is the type of result ). 然后将'unsigned int'类型的result转换为'bool'(这是result的类型)。

Note that the Standard specifies that the rank of signed int and unsigned int the same. 注意,Standard指定signed intunsigned int的等级相同。

Refer Section 4 and 5 of the C++ standard for the exact rules for conversions/promotions. 有关转换/促销的确切规则,请参阅C ++标准的第4节和第5节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM