这段代码在做什么? (size_t)-1 [英] What is this code doing? (size_t)-1
问题描述
有人可以解释当size_t或任何其他类型标识符包裹在括号中时会发生什么。我知道这是旧的typecast语法,但在这种情况下,我不跟踪发生了什么。
Can someone explain what happens when size_t, or any other type identifier, is wrapped in parentheses. I know it is the old typecast syntax but in this context I don't follow what is happening.
我已经看到它用于定义类型的最大大小as:
I've seen it for defining the max size of a type by as:
size_t max_size = (size_t)-1
推荐答案
此代码(不必要)casts -1
到 size_t
。最可能的目的是在此系统上获得 size_t
的最大可能值。
This code (unnecessarily) casts -1
to size_t
. The most possible intent was getting the largest possible value of size_t
on this system.
虽然此代码没有未定义的行为,但此代码是丑陋的 - 在C ++中,应使用 std :: numeric_limits< size_t> :: max()
,并在C中使用 SIZE_MAX
宏,目的是获取最大的 size_t
值。
Although this code doesn't have Undefined Behavior, this code is ugly - in C++ you should use std::numeric_limits<size_t>::max()
and in C use SIZE_MAX
macro for exactly a purpose of getting the largest size_t
value.
这篇关于这段代码在做什么? (size_t)-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!