C++ Logo

std-proposals

Advanced search

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

From: Tatsuya Yanagita <hemogurobin.711_at_[hidden]>
Date: Sun, 20 Mar 2022 11:34:42 +0900
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...

Received on 2022-03-20 02:34:54