我可以在C中直接比较int和size_t吗?

发布于 2024-09-17 20:42:38 字数 171 浏览 6 评论 0原文

我可以像这样比较 intsize_t 变量吗:

int i = 1;
size_t y = 2;
if (i == y) {
    // Do something
}

或者我是否必须键入强制转换其中一个?

Can I compare an int and a size_t variable like this:

int i = 1;
size_t y = 2;
if (i == y) {
    // Do something
}

Or do I have to type cast one of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2024-09-24 20:42:38

只要 int 为零或正数,它就是安全的。如果为负数,并且 size_t 的等级等于或高于 int,则 int 将转换为 size_t > 因此它的负值将变成正值。然后将这个新的正值与 size_t 值进行比较,这可能(极其不可能的巧合)给出误报。为了真正安全(也许过于谨慎),首先检查 int 是否为非负数:

/* given int i; size_t s; */
if (i>=0 && i == s)

并抑制编译器警告:

if (i>=0 && (size_t)i == s)

It's safe provided the int is zero or positive. If it's negative, and size_t is of equal or higher rank than int, then the int will be converted to size_t and so its negative value will instead become a positive value. This new positive value is then compared to the size_t value, which may (in a staggeringly unlikely coincidence) give a false positive. To be truly safe (and perhaps overcautious) check that the int is nonnegative first:

/* given int i; size_t s; */
if (i>=0 && i == s)

and to suppress compiler warnings:

if (i>=0 && (size_t)i == s)
醉殇 2024-09-24 20:42:38

如果您要将 int 类型与 size_t (或任何其他库类型)进行比较,那么您将进行危险的比较,因为 int 是有符号的,并且 size_t< /code> 是无符号的,因此其中之一将根据您的编译器/平台进行隐式转换。最好的办法是将您的 int i 重写为:

decltype(y.size()) i = 1;

这会将您的 i 指定为您要尝试比较的安全类型,我认为这是一个很好的做法。该解决方案在所有类型的迭代器中也很有用。您通常不想相信编译器会为您进行强制转换,您可以这样做,但这是有风险的,而且是不必要的风险。

If you're going to compare an int type to size_t (or any other library type), you are doing a risky comparison because int is signed and size_t is unsigned, so one of them will be implicitly converted depending on your compiler/platform. The best thing to do, is to rewrite your int i as:

decltype(y.size()) i = 1;

This assigns your i as the safe type you're trying to compare, and I think it's good practice. This solution is useful in all types of iterators as well. You generally don't want to trust the compiler to cast for you, you can, but it's risky, and an unnecessary risk.

千と千尋 2024-09-24 20:42:38

size_t 将是某种整数类型(尽管可能无符号,因此可能会生成警告),因此应该自动为您完成适当的转换。

正如其他人已经说过的那样,您可能需要重新审视生成 int 的任何计算,看看是否可以在 size_t 中进行计算(如果您正在计算 a)某物所需的尺寸。

size_t is going to be some sort of integer type (although possibly unsigned, so it might generate a warning) so the appropriate casting should be done for you automatically.

As others have already said, you may want to revisit whatever calculation is producing the int and see if you can do it in size_t in the first place if you're computing a required size for something.

别想她 2024-09-24 20:42:38

可以将 size_t 值与 int 值进行比较,int 值将隐式转换为 unsigned类型。

当您在比较中混合使用signedunsigned 类型时,某些编译器会发出警告。在这种情况下,您可以将有符号值显式转换为适当的无符号类型以抑制警告。

It is okay to compare a size_t value with an int value, the int value will be implicitly converted to unsigned type.

Some compilers will issue a warning when you mix signed and unsigned types in comparisons. In that case you can explicitly convert the signed value to an appropriate unsigned type to suppress the warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文