Is there an idiomatic C++ type for a contiguous container that lets you specify both the value type (as std::vector
and std::array
do) and the index/size type?
I've writing some code that going to manipulate a lot of arrays of data, but the indexes of the various arrays may have different semantic meanings, and I want to use the type system to prevent me from accidentally using an index in the wrong context.
For example, if I'm managing a collection of n
hats, and m
cars, I can give each hat ids from 0
up to n-1
and each car ids from 0
up to m-1
and have various arrays of hat and car information, but I wouldn't want to accidentally use a car id to look up something in an array of hat information.
car_id model_t {0};
my::vector<hat_id, fabric> hat_materials { /*...*/ }
hat_materials[model_t]; // want this to be a compile error
Right now I'm on the fence between taking the constant-time hit and using std::unordered_map
or spending some dev time wrapping std::vector
in a custom container class that adds a second parameter.