When do I need to worry about size_type?

166 Views Asked by At

I've read that using std::vector<T>::size_type is more portable than std::size_t, although size_type is usually std::size_t. For example it's more recommended to do this:

for (typename std::vector<T>::size_type i = 0; i < vec.size(); ++i)
    std::cout << vec[i] << " ";

than this:

for (std::size_t i = 0; i < vec.size(); ++i)
    std::cout << vec[i] << " ";

That seems like a lot of typing though. You can't use auto because the initializer will be deduced to int. A type alias is useless because you might as well type it out when you use it. There aren't many cases where that would actually save typing.

I tried to do something like this:

template<typename T>
struct size_type
{
    using type = typename std::vector<T>::size_type;
};

But it just ends up being typename size_type<T>::type which is just as much typing.

By far, std::size_t is the shortest thing to type. When do you actually need to worry about size_type?

0

There are 0 best solutions below