C++ Logo

std-proposals

Advanced search

Re: [std-proposals] creation function for std::vector

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Sun, 20 Mar 2022 10:40:19 +0100
On 20/03/2022 03.34, Tatsuya Yanagita via Std-Proposals wrote:
> We often create an empty std::vector with enough capacity, but it cannot be written in a single line of code.
> Thus, I would like to propose a following function:
>
> namespace std {
> template<typename T, typename Alloc = allocator<T>>
> constexpr vector<T, Alloc> make_reserved_vector(size_t capacity, const Alloc& alloc = Alloc()) {
> vector<T, Alloc> vec(alloc);
> vec.reserve(capacity);
> return vec;
> }
> }
>
> And now we can write as
>
> auto vec = std::make_reserved_vector<int>(42);
>
> I will also propose std::make_reserved_basic_string, std::make_reserved_string, std::make_reserved_wstring, etc...

Why is that better or worse than an additional constructor overload that
takes a tag type, e.g. std::vector v(std::reserve, 42)
?

Jens

Received on 2022-03-20 09:40:23