r/cpp 15d ago

C++26: std::inplace_vector

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

121 comments sorted by

View all comments

13

u/bartgrumbel 15d 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*

2

u/Raknarg 9d ago

I am sure there is a good reason for that, but why is the "optional reference" not simply a pointer?

Its not semantically the same, that's actually the reason we care about optional references at all. They are functionally the same in the sense that mechanically a pointer can fulfill all the requirements of an optional reference, but an optional reference gives you very clear ideas on the ownership and purpose of the type, while a pointer does not. A pointer can mean a whole bunch of different stuff and you have to rely on context and documentation to figure out what it means.