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.
I've seen it for defining the max size of a type as:
size_t max_size = (size_t)-1
This code (unnecessarily) casts -1
to size_t
. The most probable intent was getting the largest possible value of size_t
on this system.
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
is in fact the equivalent of size_t(-1)
See also the following question c cast syntax styles
Some library methods intentionally return (size_t)(-1)
to indicate an error condition. For example, the iconv method from the GNU libiconv library . I assume there is some good reason why these functions don't return ssize_t
(signed) return values, which would allow you to check for -1 directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.