C++ Logo

std-discussion

Advanced search

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

From: Wilhelm Meier <wilhelm.meier_at_[hidden]>
Date: Thu, 14 Nov 2019 07:29:30 +0100
Am 09.11.19 um 17:01 schrieb Thiago Macieira via Std-Discussion:
> On Friday, 8 November 2019 23:23:59 PST Wilhelm Meier via Std-Discussion
> wrote:
>> Well, on one side this is understandable, but on the other side an
>> implementation für small µC could get some benefit from declaring
>> size_type to the small unsigned integer possible for the actual size of
>> the array. And I don't see any obstackles doing so.
>
> I don't see the point. If you want to *store* the information, you're not
> using std::array, it's your choice. If you want to *manipulate* the size, then
> integer promotion rules apply anyway and you're going to get either int or
> unsigned, which in your microcontroller is likely going to be the same size as
> size_t.
>

The point is to use

std::array<A, 5> a;

auto x{a.size()}; // uint8_t

or

std::array<A, 5>::size_type y{}; // uint8_t

In highly templated code the size_type "propagates" through the
templates and produces optimal code by choosing the "right" type in all
places.

Received on 2019-11-14 00:31:52