C++ Logo

std-discussion

Advanced search

std::usize()?

From: Marc Mutz <marc.mutz_at_[hidden]>
Date: Mon, 27 Mar 2023 11:35:59 +0200
Hi,

We've been wondering why there's
- std::size(), returning the Container::size_type
- std::ssize(), returning make_signed_t<Container::size_type>

but not
- std::usize(), returning make_unsigned_t<Container::size_type>

Does anyone know whether this was discussed?

In particular, Qt containers have had signed size_type since their
inception (preceding the STL, btw) and while we can use std::ssize() to
coerce STL containers to use signed sizes, we can't easily coerce Qt
containers to use unsigned sizes, as std::size() will always use the
Container's size_type:

    QList<int> l = ~~~;
    std::vector<int> v;
    v.reserve(l.size() * 2); // warns
    v.reserve(static_cast<size_t>(l.size() * 2)); // ok, but verbose
         // and if decltype(l) ever becomes an STL container, issues a
warning about an unnecessary cast
    v.reserve(std::usize(l)); // <- wanted

Thanks,
Marc

Received on 2023-03-27 09:36:03