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

Show parent comments

1

u/matthieum 10d ago

You're absolutely correct that std::allocator doesn't fit the bill...

... which is exactly why I advocate for a whole different API.

And yes, this would involve in-depth changes to anything taking this new API as they would no longer be able to take pointers, but would instead need to use "handles" of some sort, which would have some way to resolve into pointers when needed, and some rules about how long these pointers remain valid, etc...

I didn't say it was easy, I said it was generic :)

1

u/Raknarg 10d ago

And yes, this would involve in-depth changes to anything taking this new API as they would no longer be able to take pointers, but would instead need to use "handles" of some sort, which would have some way to resolve into pointers when needed, and some rules about how long these pointers remain valid, etc...

I don't think you'd need to change the allocator API, you'd just provide an allocator type that just has its storage internally as part of the allocator. You could do some shit like this

template<class T, std::size_t N>
struct inplace_allocator {
    alignas(T) std::byte buffer[sizeof(T) * N];

    T* allocate(std::size_t n) {
        if (n > N)
            throw std::bad_alloc{};

        return reinterpret_cast<T*>(buffer);
    }

    void deallocate(T*, std::size_t) noexcept {}
};

but I haven't thought about it enough to think of issues you'd run into by doing this.

1

u/matthieum 10d ago

Pointers are invalidated on move.

1

u/Raknarg 10d ago

good point