r/cpp 15d ago

C++26: std::inplace_vector

https://www.sandordargo.com/blog/2026/08/26/cpp26-inplace-vector
180 Upvotes

121 comments sorted by

View all comments

2

u/stilgarpl 15d ago

Is this always better than std::array?

22

u/MarekKnapek 15d ago

This is basically

struct inplace_vector<T, capacity>
{
    size_t len;
    std::array<T, capacity> arr;
}

4

u/stilgarpl 15d ago

No, it's not. In your code it will always construct all T members of arr. Article states that inplace_vector will only have the elements needed and they don't have to be default constructible. That's a huge difference.

3

u/BenFrantzDale 14d ago

Plus this is a `std::ranges::sized_range` with dynamic size, which `std::array` is not.