简体   繁体   English

警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare]

[英]warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Warning in below line以下行中的警告

for(int nPort = 0 ; nPort< (sizeof(nArrOverloadParams)/sizeof(int)) && nRetVal 
                    == RET_SUCCESS_VALUE ;nPort++)

The warning indicates that the comparison in principle could fail.该警告表示原则上比较可能失败。 The signed value is (1) promoted to the unsigned type.有符号值被(1)提升为无符号类型。 And if it's negative (the compiler doesn't know that it isn't, without extensive analysis) then the promotion to unsigned produces a very large value.如果它是负的(编译器不知道它不是,没有广泛的分析)那么提升到 unsigned 会产生一个非常大的值。

A consequence is that you're guaranteed that结果是你可以保证

std::string( "Blah" ).length() < -5

which is just extremely silly – it's a consequence of a choice of type for size values that once made sense, but that that currently is pretty sub-optimal (but impossible to change due to the need for compatibility).这简直太愚蠢了——这是为曾经有意义的大小值选择类型的结果,但目前这是非常次优的(但由于需要兼容性而无法更改)。

As a rule of thumb it's therefore a good idea to因此,根据经验,这是一个好主意

  • use signed types for numbers, but对数字使用有符号类型,但

  • use unsigned types for bit level operations.使用无符号类型进行位级操作。

Eg you can use ptrdiff_t , the type of the result of a pointer difference expression, as a signed version of size_t .例如,您可以使用ptrdiff_t ,指针差异表达式的结果类型,作为size_t有符号版本。

Then you can express a signed size function for arrays as eg然后,您可以将数组的有符号大小函数表示为例如

#include <stddef.h>

namespace cppx {
    using Size = ptrdiff_t;

    template< class Item, Size n >
    auto n_items( Item (&)[n] )
        -> Size
    { return n; }
}

and the relevant part of the loop then becomes然后循环的相关部分变成

using cppx::n_items;

int overloadedParams[77];
for( int port = 0 ; port< n_items( overloadedParams ); port++ )

An alternative is to use a range-based loop , bypassing the entire issue:另一种方法是使用基于范围的循环,绕过整个问题:

for( int param : overloadedParams )

If the index is needed for the loop body's logic then a counter can be added.如果循环体的逻辑需要索引,则可以添加一个计数器。

The behavior of the loop is defined in terms of equivalent code expressed in terms of std::begin and std::end .循环的行为是根据用std::beginstd::end表示的等效代码定义的。


History.历史。 As far as I know it was Dietmar Kuhl who identified the triad of functions begin , end and n_items as practically useful (it's the necessary support for signed size programming), although not with that name for the third function.据我所知,是 Dietmar Kuhl 将beginendn_items函数三元组确定为实际有用(这是对有符号大小编程的必要支持),尽管第三个函数没有使用该名称。 The C++11 standard added std::begin and std::end , but no std::n_items ( std::extent is a very different beast). C++11 标准添加了std::beginstd::end ,但没有添加std::n_itemsstd::extent是一个非常不同的野兽)。


1) In the standard this conversion is part of the “usual arithmetic conversions” defined by C++14 §5/10. 1)在标准中,此转换是 C++14 §5/10 定义的“常用算术转换”的一部分。

暂无
暂无

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

相关问题 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] C++ 错误 - 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - C++ Error - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 我的 For 循环有什么问题? 我收到警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - What is wrong with my For loops? i get warnings: comparison between signed and unsigned integer expressions [-Wsign-compare] 警告 - 有符号和无符号整数表达式之间的比较 - A warning - comparison between signed and unsigned integer expressions 在进行make install时,有符号和无符号整数表达式之间的比较警告? - comparison between signed and unsigned integer expressions warning while make install? 警告:有符号和无符号整数表达式之间的比较..如何解决? - warning: comparison between signed and unsigned integer expressions..how to solve it? 有符号和无符号整数表达式S之间的比较 - Comparison between signed and unsigned integer expressions S -gsign-compare警告用g ++表示 - -Wsign-compare warning in g++ 有符号和无符号整数表达式的比较 - Comparison between signed and unsigned integer expressions 警告:有符号和无符号整数表达式之间的比较 - Warning: Comparison between signed and unsigned integer expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM