I'm writing C++ code, where the standard library has an idiomatic type for representing sizes: std::size_t
.
Now, I'm writing a function which counts certain kinds of objects; and these objects have indices, used as their id's, which start from 0, with some indices in the middle possibly skipped due to device issues - although in practice they are never missing. Say these objects's index type is idx_t
. Also, to make this question non-trivial, id_t
is typically different, smaller than than std::size_t
. It is guaranteed that the number of objects isn't 2^sizeof(idx_t)
, i.e. there are idx_t
values to spare.
My "philosophical" design question: Which type should a count_objects()
function return:
- a
std::size_t
, the standard size type, or - an
idx_t
, the objects' numeric index type?
Please argue in favor of your choice.
Notes:
- The objects' index type is a constraint, and cannot be altered, e.g. due to interaction with a device driver or third-party library.
- I believe this question isn't really C++ specific, but I wanted to make it less vague, so I didn't generalize much.
idx_t
might beint
, orunsigned
, orshort
, orstd::int16_t
, orstd::uint16_t
, orstd::uint32_t
, or orstd::int32_t
etc.
std::size_t
is conventional; in the code for working with these objects, it's never used.