C++ Logo

std-proposals

Advanced search

Re: Request for opinion: Leaning on strong typing in STL.

From: Nevin Liber <nevin_at_[hidden]>
Date: Wed, 22 Jul 2020 18:49:09 -0500
On Wed, Jul 22, 2020 at 12:51 PM Scott Michaud via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> One solution (for C++) is to make an additional, explicit version of the
> constructor that has intent-declaring parameters (which can be viewed in
> the IDE's overload list). In this specific case, my initial thoughts would
> be to create a std::reserve structure (and possibly a
> std::default_initialize structure, etc.).
>
> I briefly discussed this on the #Include C++ Discord channel, and feedback
> was mostly positive. One person suggested converting the constructor to a
> two-parameter version, which avoids creating the structure. I think I like
> their idea better, especially since it frees std::reserve from always being
> a size_t container,
>
I'm generally in favor of this. It is a fairly small addition, and is very
useful in member initializer lists (where, say, using a lambda would
require spelling out the vector type).

Another way to do this is with struct reserve templated on the size type.
I mention this not because I prefer it, but a proposal should explore the
design space.

I also have a slight preference towards the two parameter constructor,
declared as:

struct reserve_t { constexpr explicit reserve_t() = default; };
inline constexpr reserve_t reserve;

The explicit default constructor keeps it from being called as vector({},
2);
-- 
 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  +1-847-691-1404

Received on 2020-07-22 18:53:03