What is this code doing? (size_t)-1

前端 未结 3 1668
深忆病人
深忆病人 2021-02-14 08:12

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 w

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 08:42

    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::max() and in C use SIZE_MAX macro for exactly a purpose of getting the largest size_t value.

提交回复
热议问题