C++: Is there any reason to use uint64_t instead of size_t

c++, integer, size-t, vector

Solution

uint64_t is guaranteed to be 64 bits. If you need 64 bits, you should use it.

size_t isn't guaranteed to be 64 bits; it could be 128 bits in a future machine. So, keyword uint_64 its reserved by that :)

Problem

My understanding of `size_t` is that it will be large enough to hold any (integer) value which you might expect it to be required to hold. (Perhaps that is a poor explanation?) For example, if you were using something like a for loop to iterate over all elements in a vector, `size_t` would typically be 64 bits long (or at least on my system) in order that it can hold all possible return values from vector.size(). Or at least, I think that's correct? Therefore, is there any reason to use A rather than B: A: `for(uint64_t i = 0; i < v.size(); ++ i)` B: `for(size_t i = 0; i < v.size(); ++ i)` If I'm wrong with my explanation or you have a better explanation, please feel free to edit. Edit: I should add that my understanding is that `size_t` behaves like a normal unsigned integer - perhaps that is not correct?

Original source