Given the following piece of code:
template<typename T>
class MyContainer
{
typedef T value_type;
typedef unsigned int size_type;
...
};
How one should initialize variables using size_type (like loop indexes)?
Should it be:
for(size_type currentIndex = size_type(0);currentIndex < bound;++currentIndex)
or
for(size_type currentIndex = static_cast<size_type>(0);currentIndex < bound;++currentIndex)
The rationale for the question is to produce code that will still work when type underlying size_type is changed or added to template parameters.
Thanks…
There are four possibilities I see:
I would prefer the last one. It’s concise, and has the same effect as the rest.
You’re probably worried that if the type change this won’t work, or something. The thing is,
size_type‘s are, by convention, unsigned integers. 0 is always going to be a valid value as long assize_typeis a sensible & correct size-measuring type.