C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Why is size_type in std::array an alias for size_t?
From: Wilhelm Meier (wilhelm.meier_at_[hidden])
Date: 2019-11-14 00:29:30


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.


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups