Why is std::size_t used when creating function template that takes std::array?

362 Views Asked by At
template <typename T, std::size_t size>
void printArray(const std::array<T, size>& myArray)
{
    for (auto element : myArray)
        std::cout << element << ' ';
    std::cout << '\n';
}

Why is std::size_t being used? What are the benefits of size_t vs unsigned int?

1

There are 1 best solutions below

0
Red.Wave On

std::size_t as its name clearly shows, is the standard type to store values for sizes of objects. It is the alaise of largest unsigned integer used by current standard(std::uint64_t) and can represent the largest possible virtual size. I don't find it practical to load a multi-GB file as a byte array in RAM; but if that's what must be done and the workstation can have a sufficiently large amount of RAM, then no other fundamental built-in type is capable of storing the size of resulting array or a proper index into it.