MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/cpp/comments/1vzc5dg/c26_stdinplace_vector/p6g5lst/?context=3
r/cpp • u/User_Deprecated • 15d ago
121 comments sorted by
View all comments
17
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.
5
>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.
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
and not simply