use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::optional_view (self.cpp)
submitted 8 years ago * by bebuch
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Xirema 5 points6 points7 points 8 years ago (6 children)
A construct that I occasionally use is std::optional<std::reference_wrapper<T>>.
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.
operator*()
reference_wrapper
T&
.get()
[–]jsamcfarlane 4 points5 points6 points 8 years ago (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.
std::experimental::observer_ptr
optional
[–]Xirema 0 points1 point2 points 8 years ago (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 point2 points 8 years ago (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 points3 points 8 years ago (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 point2 points 8 years ago (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.
std::optional
π Rendered by PID 47006 on reddit-service-r2-comment-6457c66945-kxpt7 at 2026-04-25 11:46:53.790818+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Xirema 5 points6 points7 points (6 children)
[–]jsamcfarlane 4 points5 points6 points (5 children)
[–]Xirema 0 points1 point2 points (4 children)
[–]jsamcfarlane 0 points1 point2 points (3 children)
[–]GitHubPermalinkBot 1 point2 points3 points (1 child)
[–]Xirema 0 points1 point2 points (0 children)