C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal for std::optional extensions

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Sun, 9 Jun 2024 21:18:57 +0200
Hello,

On 09/06/2024 14:44, Lorenz Quack via Std-Proposals wrote:
> 1. Allow passing pointer-to-member (both data and function member) to std::optional::transform()
> Example:
> // before proposal
> int x = try_generate_foo().transform([](Foo& f){ return f.bar; });
>
> // after proposal
> int x = try_generate_foo().transform(&Foo::bar);
>
> This is similar to how the projections of the ranges algorithms allow pointer-to-members to be passed.
> If this proposal idea goes further it should be considered whether the exact same INVOKE semantics [1] should
> apply here.

These writings look a bit off: transform has to return another optional,
not a value. Did you mean to write "optional<...> x"?

In any case, transform today is *already* specified to use std::invoke /
invoke_result_t. What stops the second statement from Just Working™ is
the fact that, for it to have any sense, it has to return an optional
*reference* and we don't have those yet.

(In fact, note that the two statements above are not equivalent.)

Therefore, there's this Mandates clause in place that blocks that condition:

> https://eel.is/c++draft/optional#monadic-11

> Mandates: U is a non-array object type

which is false, U would be `int &&` and that's not an object type.


> 2. Add the ability to call a function to create a default value.

https://github.com/cplusplus/papers/issues/921

Was proposed as value_or_else.


My 2 c,
--
Giuseppe D'Angelo

Received on 2024-06-09 19:19:01