# Implementing P3413 A more flexible optional::value\_or (else!) in beman.optional

**URL:** <https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399>\
**Category:** Beman Libraries\
**Created:** [April 14, 2025, 3:39pm UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399 "2025-04-14T15:39:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [April 14, 2025, 3:39pm UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399/1 "2025-04-14T15:39:34Z")

</div>

In particular for @Sdowney

There’s some ongoing committee reflector discussion of additional optional capabilities – including this proposal:

> <https://github.com/cplusplus/papers/issues/2084>
>
> \[P3413R0\](https://wg21.link/p3413r0) A more flexible optional::value\_or (else!) …(Corentin Jabot)

This seems like a great project for C++Now Library in a Week that someone could implement on top of the current beman.optional. I think, as usual, this might reignite the discussion about how that should be done – new repo, in the existing repo? My $.02 is it should be done in the existing repo with an option that the feature can be removed by a build setting if desired – although I’d be fine with it just always being there.

Thoughts?

---

<div class="post-metadata">

**Author:** ![Sdowney](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/sdowney/32/73_2.png) [@Sdowney](https://discourse.bemanproject.org/u/Sdowney)\
**Post date:** [April 18, 2025, 8:59am UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399/2 "2025-04-18T08:59:57Z")

</div>

I will of course read the fine paper before criticising too heavily, but I do know that trying to change the signature of value\_or broke some existing code when I tried. So I’d want strong evidence of non-breaking for any changes. Optional is unfortunately fairly delicate.

I haven’t touched it recently since it was not going to be feasible at that point for 26, but I think the right answer is some free functions in this space for `nullable` or `maybe` kinds that would include raw pointers and would use the common reference type machinery from ranges to compute the return type, as well as the constructs/converts from temporary checks.

P1255 had some work towards this in the most recent version.

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [April 18, 2025, 10:19am UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399/3 "2025-04-18T10:19:25Z")

</div>

To be clear, the paper proposes pure additions and doesn’t attempt to change the existing methods. And to make it easy for you it’s literally just this:

```auto
emplate<class ... Args> constexpr T value_or_construct( Args &&... args ) const &;
template<class ... Args> constexpr T value_or_construct( Args &&... args ) &&;
template<class U, class ... Args>
constexpr T value_or_construct (initializer_list <U> il, Args&&... args>) const &;
template<class U, class ... Args>
constexpr T value_or_construct (initializer_list <U> il, Args&&... args>) &&;
template <class F> constexpr T value_or_else (F&& f) const &;
template <class F> constexpr T value_or_else (F&& f) &&

```

Some of this comes from _jealousy of expected_ which got a couple other monadics since people made a case for them as the paper ran through the committee.

If you haven’t seen it already, have a search on the lib-ext reflector for Nico’s post with optional or\_else.

---

<div class="post-metadata">

**Author:** ![Sdowney](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/sdowney/32/73_2.png) [@Sdowney](https://discourse.bemanproject.org/u/Sdowney)\
**Post date:** [April 18, 2025, 5:53pm UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399/4 "2025-04-18T17:53:18Z")

</div>

I have to sit down with Corentin and hash this out, but I want all of these, but also for a plain T\*.  
Anything dereferenceable and contextually boolean, really. Steal the Maybe monad to go along side the List monad we stole for ranges. Excuse me, “borrowed”.  
I have a nice number on hold for trees and traversable, as well.

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [April 18, 2025, 6:25pm UTC](https://discourse.bemanproject.org/t/implementing-p3413-a-more-flexible-optional-value-or-else-in-beman-optional/399/5 "2025-04-18T18:25:40Z")

</div>

Fair, but those other ones wouldn’t really prevent these being added by a volunteer at the conference 🙂 – under some sort of experimental or feature test mode. In any case we can discuss more in aspen
