C++ Logo

std-discussion

Advanced search

Re: Why is size_type in std::array an alias for size_t?

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sat, 9 Nov 2019 11:41:30 +0300
On 2019-11-09 10:45, Wilhelm Meier via Std-Discussion wrote:
> Am 09.11.19 um 08:27 schrieb Peter C++:
>> If you know that your size_type values always fit into an uint_8t you can store it there if needed, eg, to iterate. Std::array never keeps its size somewhere at run time, so I fail to see the issue.
>
> I don't want to store the actual size inside the array. I just want der
> size_type declaration to be adaptive:
>
> using size_type = std::condifition_t<(Size < 256), uint8_t,
> std::conditional_t<(Size < 65536), uint16_t, ... >>>;
>
> So a user of std::array can use the smallest unsigned int wenn using
> size_type.

What's the point of doing this? You can already use whatever type you
want to store the array size outside the array.

Changing the type returned by array::size() can have surprising effects
if that type is propagated e.g. as a template argument of a function
call. "Surprising effects" include suboptimal code (due to having to
zero extend integers) and unintended overflows.

Received on 2019-11-09 02:43:53