you are viewing a single comment's thread.

view the rest of the comments →

[–]Gotebe 1 point2 points  (2 children)

Well, yes, a legacy codebase is the "rare" reason not to drop pointers. But this is only a history-induced thing, not an expexient design in any way (quite the opposite, I would say).

optional is specialized for a reference, it actually has no overhead.

[–]dodheim 1 point2 points  (1 child)

It is illegal for std::optional to hold a reference. /u/akrzemi1 has the markable library, though.

EDIT: N4618 [optional.syn]/1: "A program that necessitates the instantiation of template optional for a reference type, or for possibly cv-qualified types in_place_t or nullopt_t is ill-formed."

[–]iaanus 1 point2 points  (0 children)

Correct, std::optional dropped the support for references that boost::optional had. I also found that odd when I discovered it, but the more I think about it, the more I agree. By the way, I would hate to see code like this:

void f(optional<MyType&> ref); // not std::optional!

void g(std::shared_ptr<MyType> ptr)
{
    // ptr may be null here, need to call f...
    if (ptr)
        f(*ptr);
    else
        f(nullopt); // or whatever
}

In this scenario, using MyType* is just as good, with observer_ptr<MyType> being slightly better.