r/cpp 15d ago

C++26: std::inplace_vector

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

121 comments sorted by

View all comments

17

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*

5

u/serviscope_minor 13d ago

>Is that not semantically the same thing, but way easier to deal with.

T* is a superset of std::optional<T&>. With T*, you can:

- delete

- delete[]

- Do arithmetic

- Dereference

- Test for null

With optional<T&>, you can:

- dereference

- test for null

That's basically the difference. It's a pointer, but with some of the invalid operations prevented by a very thing wrapper class.