r/cpp 6d ago

C++26: std::inplace_vector

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

121 comments sorted by

View all comments

14

u/bartgrumbel 6d ago

I am sure there is a good reason for that, but why is the "optional reference" not simply a pointer? Is that not semantically the same thing, but way easier to deal with.

I.e. why

std::optional<T&>

and not simply

T*

9

u/SyntheticDuckFlavour 5d ago

If you return me a pointer, what does that communicate to me? Who owns it? Is nullptr an error or something I should expect? Is this documented somewhere? The advantage of std::optional is explicit about intent. I just have to look at the API and I know immediately what the author intended with the return value.

3

u/bartgrumbel 4d ago

Who owns it?

All good points, and this one in particular. Never though about it, but sure, a returned pointer might imply returned ownership, whereas an (optional) reference never means that. Thanks!

1

u/jk-jeon 4d ago

a returned pointer might imply returned ownership

It does not. Unless the library is from the era of C++98/03 (or before), or the author hates the users. Or if there is a very specific reason.

8

u/serviscope_minor 4d ago

>It does not. Unless[...]

Well that's the thing isn't it? The unless has some pertty huge carve outs.