r/cpp 6d ago

C++26: std::inplace_vector

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

121 comments sorted by

View all comments

3

u/stilgarpl 6d ago

Is this always better than std::array?

19

u/MarekKnapek 6d ago

This is basically

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

5

u/drjeats 6d ago

Oh.

I thought this was the way-more-useful thing where you give it an in-place capacity and it allocates once it exceeds that.

Like this is also useful, but it's kinda trivial to roll your own of this.

4

u/KuntaStillSingle 5d ago

I thought this was the way-more-useful thing where you give it an in-place capacity and it allocates once it exceeds that

pmr vector using monotonic buffer does this with the default upstream memory resource:

https://en.cppreference.com/cpp/memory/monotonic_buffer_resource/monotonic_buffer_resource

2

u/drjeats 5d ago

TIL, that's useful.

We deserve a non-pmr pre-sized SBO vector derived from std::vector though.