Signed Size
Int (i.e. signed) as type for *.size()
- Because mixing signed and unsigned integer (e.g. “signed - unsigned”) and even calculating “unsigned - unsigned” is difficult to handle.
- Problem in C/C++:
aUInt - 1 >= 0is always true (even ifaUIntis0)
- Problem in C/C++:
- When working with sizes, calculating the difference is common; Then you are limited to
Int//SSize(i.e. signed integer) anyway.PtrDiff - Anyone who needs more than 2GB of data in a single “byte array”, should please use a 64 bit platform.
- For bounds checking, the two comparisons
x >= 0andx < widthmay very well be reduced to a singleUInt(x) < widthby the compiler in an optimization step. - Then types
andSize/SSizeare not necessary anymore, so two/three types less.PtrDiff- We simply use
Intinstead. UIntis used in rare cases (i.e. hardware registers, masks, flags), surely not for sizes.
- We simply use
- See also Going Native 2012, Day 2, Interactive Panel: Ask Us Anything
- 42:41 - 45:28
- Bjarne Stroustrup and Herb Sutter recommend using signed integer.
- 1:02:51 - 1:03:14
- Herb Sutter and Chandler Carruth about unsigned
size_tin the C++ STL containers: “They are wrong”, “We are sorry”
- Herb Sutter and Chandler Carruth about unsigned
- 42:41 - 45:28