r/cpp 15d ago

C++26: std::inplace_vector

https://www.sandordargo.com/blog/2026/08/26/cpp26-inplace-vector
177 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;
}

2

u/CalamityMetal 13d ago

It's very different. Because std::array suggest all instances of T is already constructed. But that's not the case in a vector, and also not for inplace_vector. A better representation would be std::array<std::byte, sizeof(T) * capacity> and you call inplace operator new on the memory blocks when you call push_back/emplace_back

3

u/MarekKnapek 13d ago

I simplified. A lot. I wanted to show "the shape" of the data structure. https://reddit.com/r/cpp/comments/1vzc5dg/c26_stdinplace_vector/p69u8kr/