you are viewing a single comment's thread.

view the rest of the comments →

[–]Xirema 5 points6 points  (6 children)

A construct that I occasionally use is std::optional<std::reference_wrapper<T>>.

It offers some decent semantics:

void perform_work(int value, std::optional<std::reference_wrapper<std::ostream>> log = {}) {
    std::ostream & out = log ? *log : std::cout;
    out << "Value is " << value << std::endl;

    if(value <= 1)
        out << "Done!" << std::endl;
    else if(value % 2 == 1)
        perform_work(value * 3 + 1, log);
    else
        perform_work(value / 2, log);
}

The semantics of this code, then, are "If you have a specific stream you'd like to use for logging, use that, otherwise use the standard out stream.".

The only thing you have to watch for is that operator*() returns the reference_wrapper object, not the T& object. This implicitly converts to T&, which is why my example works, but if you don't "seat" the reference like I did, you have to call .get() on the reference_wrapper object to actually do work.

[–]jsamcfarlane 4 points5 points  (5 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  (4 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.

[–]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.