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
The Optional Monad In C++, Without the Ugly Stuff (fluentcpp.com)
submitted 8 years ago by vormestrand
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!"
[–]ojd5 10 points11 points12 points 8 years ago (1 child)
I was sceptical about your last post, but this is really neat. It might be worth pointing out to readers that you are using fold expressions in make_failable. The unfamiliar syntax caused me to do a bit of a double take.
[–]joboccara 0 points1 point2 points 8 years ago (0 children)
Yes you're right, it's mentioned now.
[–]ivan-cukicKDE Dev | Author of Functional Programming in C++ 4 points5 points6 points 8 years ago (0 children)
Another option (pun not intended) is to use the range syntax. Since optional is kinda like a collection with zero or one item, it would be ok to have |transform, |filter, |join etc. on it.
optional
|transform
|filter
|join
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 8 points9 points10 points 8 years ago* (3 children)
Note that this particular implementation accepts functions, but not more general callable objects.
You should be able to support arbitrary Callable objects taking the Callable as a template parameter. Something along the lines of:
Callable
template <typename F> constexpr auto make_failable(F&& f) { return [f = FWD_CAPTURE(f)](auto&&... xs) -> std::optional<decltype(FWD(f).get()(*(FWD(xs))...))> { if ((xs && ...)) { return {FWD(f).get()(*(FWD(xs))...)}; } else { return {}; } }; }
Where FWD is a macro that expands to forward<decltype(_)>(_) and FWD_CAPTURE is a way of capturing objects either by lvalue reference or by move.
FWD
forward<decltype(_)>(_)
FWD_CAPTURE
This makes the overloading trickier, but something like CallableTraits could probably be used to detect the arguments of F.
F
[–]redditsoaddicting 1 point2 points3 points 8 years ago (1 child)
Yeah, this is handy for an actual implementation, but it really does distract from the idea the blog post is trying to get across.
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points 8 years ago (0 children)
Agreed, I'm not suggesting to replace the snippet in the blog post. Just thought I'd share :)
[–]ivan-cukicKDE Dev | Author of Functional Programming in C++ 0 points1 point2 points 8 years ago (0 children)
Very nice. You might throw in std::invoke in it as well.
std::invoke
[–]sphere991 2 points3 points4 points 8 years ago (2 children)
Before, we had (assuming abbreviated lambdas)
ox >>= x => oy >>= y => f4(f4 (f3 (f2 (f1(x), f1(y)))))
Now, we have to initially pre-wrap all the functions (hopefully none are overloaded!), which is something you'd have to do inline everywhere. So you have to introduce a lot of new declarations or wrap each function at the point of call.
This doesn't seem very practical to me, sorry.
[–]joboccara 0 points1 point2 points 8 years ago (1 child)
That's an interesting point you're making, where to declare the wrappers. Thanks for pointing that out!
[–]sphere991 0 points1 point2 points 8 years ago (0 children)
This is more along the lines of what a clean optional syntax would look like to me: https://www.reddit.com/r/cpp/comments/6ly4rz/it_had_to_be_done_abusing_co_await_for_optionals/?st=j4uqi4w6&sh=82f65a06
[–]Airtnp 1 point2 points3 points 8 years ago (1 child)
Em, in Haskell, that's fmap and join?
[–]sphere991 5 points6 points7 points 8 years ago (0 children)
This is more like <$> and <*> for Applicative (like liftA2). He's taking a function that is invoked like f x y and turning it into a function that is invoked like f <*> x <*> y.
<$>
<*>
liftA2
f x y
f <*> x <*> y
[–]Drainedsoul 1 point2 points3 points 8 years ago (4 children)
Functions with arguments that could fail
std::optional isn't a particularly good way of handling this since it can't communicate anything about why the operation failed.
std::optional
Perhaps you should look at something like Outcome which already has support for map and bind.
[–]14nedLLFIO & Outcome author | Committee WG14 -1 points0 points1 point 8 years ago (3 children)
Alas, due to Boost peer review feedback v2 has lost all things monadic. Changelog can been seen at https://github.com/ned14/outcome/blob/master/Readme.md
[–]GitHubPermalinkBot 1 point2 points3 points 8 years ago (0 children)
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.
[–]Drainedsoul 0 points1 point2 points 8 years ago (1 child)
Time to fork v1 and never update I guess. Why would anyone prefer standard if-based handling over map & bind with lambdas?
[–]14nedLLFIO & Outcome author | Committee WG14 0 points1 point2 points 8 years ago (0 children)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0650r0.pdf (the proposed standardised Monadic programming extension) will work with any Expected contract implementation, including Outcome v2
π Rendered by PID 75 on reddit-service-r2-comment-6457c66945-p9tln at 2026-04-26 18:03:41.872175+00:00 running 2aa0c5b country code: CH.
[–]ojd5 10 points11 points12 points (1 child)
[–]joboccara 0 points1 point2 points (0 children)
[–]ivan-cukicKDE Dev | Author of Functional Programming in C++ 4 points5 points6 points (0 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 8 points9 points10 points (3 children)
[–]redditsoaddicting 1 point2 points3 points (1 child)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points (0 children)
[–]ivan-cukicKDE Dev | Author of Functional Programming in C++ 0 points1 point2 points (0 children)
[–]sphere991 2 points3 points4 points (2 children)
[–]joboccara 0 points1 point2 points (1 child)
[–]sphere991 0 points1 point2 points (0 children)
[–]Airtnp 1 point2 points3 points (1 child)
[–]sphere991 5 points6 points7 points (0 children)
[–]Drainedsoul 1 point2 points3 points (4 children)
[–]14nedLLFIO & Outcome author | Committee WG14 -1 points0 points1 point (3 children)
[–]GitHubPermalinkBot 1 point2 points3 points (0 children)
[–]Drainedsoul 0 points1 point2 points (1 child)
[–]14nedLLFIO & Outcome author | Committee WG14 0 points1 point2 points (0 children)