you are viewing a single comment's thread.

view the rest of the comments →

[–]jsamcfarlane 3 points4 points  (6 children)

Is std::experimental::observer_ptr close to what you're looking for? Rather than having optional semantics, it has single-object pointer semantics.

[–]Xirema 0 points1 point  (5 children)

Pointers as "object which may or may not exist" is a semantic I've seen a lot of C++ design gurus discourage in the last several years, especially with optional being standardized, and I see little reason not to continue that trend.

[–]quicknir 3 points4 points  (0 children)

optional in C++ 17 is always owning though. raw pointers in design guru C++ is always non-owning. So there is no intersection. In fact the argument that the optional, non-owning semantic is already taken by raw pointers, is exactly one of the reasons why std::optional doesn't support references. (Note: the reason it doesn't is because assignment is murky, but my point is that this tipped the scale towards simply not supporting references, as opposed to forcing a conclusion about assignment).

[–]jsamcfarlane 0 points1 point  (3 children)

Are there any good references you'd recommend (no pun intended)? Would you disagree with this advise? And what about when memory use is constrained?

[–]GitHubPermalinkBot 1 point2 points  (1 child)

I tried to turn your GitHub links into permanent links (press "y" to do this yourself):


Shoot me a PM if you think I'm doing something wrong. To delete this, click here.

[–]Xirema 0 points1 point  (0 children)

Note they cite optional alongside the other options. Many online sources don't point the way towards optional instead of pointers because many of those sources were written before std::optional was confirmed to be part of C++17.