Bjarne Stroustrup's observation that "using unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea" has raised concerns about the decision to make size_t unsigned.
Originally, size_t was made unsigned to accommodate for architectures with 16-bit pointers, such as DOS systems. To prevent string size limitations, the C standard mandated ptrdiff_t, the signed counterpart of size_t, to be effectively 17 bits.
While these historical reasons may still be applicable in embedded systems, they are less so for modern 32-bit and 64-bit programming. In these environments, there are no practical advantages to using unsigned types for numbers, and their use can introduce potential bugs due to C/C implicit conversion rules (e.g., where string("Hi").length()
The decision to make size_t unsigned was not a mistake, but rather a practical choice for the limited systems of its time. However, in modern programming practices, it is generally advisable to minimize the use of unsigned integers in interfaces and for numbers, except for specific circumstances where the self-descriptive nature of typedef int MyType is beneficial.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3