There are all kind of monadic expressions for testing for a value or error:

 

https://en.cppreference.com/w/cpp/utility/optional/value_or

https://en.cppreference.com/w/cpp/utility/optional/or_else

https://en.cppreference.com/w/cpp/utility/optional/and_then

https://en.cppreference.com/w/cpp/utility/optional/transform

 

https://en.cppreference.com/w/cpp/utility/expected/value_or

https://en.cppreference.com/w/cpp/utility/expected/or_else

https://en.cppreference.com/w/cpp/utility/expected/and_then

https://en.cppreference.com/w/cpp/utility/expected/transform

https://en.cppreference.com/w/cpp/utility/expected/transform_error

 

Also there is a proposal for operator?? renamed try?

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2561r2.html


 

-----Ursprüngliche Nachricht-----
Von: Lauri Vasama via Std-Proposals <std-proposals@lists.isocpp.org>
Gesendet: Do 31.10.2024 11:01
Betreff: Re: [std-proposals] Calling methods on a nullptr
An: std-proposals@lists.isocpp.org;
CC: Lauri Vasama <wg21@vasama.org>;
This sounds like the null conditional operator of C#.

In C# x?.f() (as opposed to just x.f() ) returns null if x is null, and
otherwise evaluates x.f().

I could see this being a useful new operator in C++, though nullability
is more varied in C++. Would it work only for raw pointers? Or also for
smart pointers? What about optionals?

On 29/10/2024 22.20, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Tue, Oct 29, 2024 at 5:01 PM Jeremy Rifkin via Std-Proposals wrote:
>> Do you have a concrete example of a problem that could be uniquely
>> solved by being able to call a member function on a null pointer? My
>> gut feeling is that this is an anti-pattern and symptom of bad design
>>   however, I might not be considering all possible use cases.
>
> Well what came to mind for me first was where you have code like the following:
>
> string GetCodecName(Manager *const pmanager)
> {
>      if ( nullptr == pmanager ) return {};
>      auto *const pdev = pmanager->GetDevice();
>      if ( nullptr == pdev ) return {};
>      auto *const pchannel = pdev->GetChannel(0);
>      if ( nullptr == pchannel ) return {};
>      auto *const plow = pchannel->GetLowPassFilter();
>      if ( nullptr == plow ) return {};
>      return plow->GetCodecName();
> }
>
> You could replace it with:
>
> string GetCodecName(Manager *const pmanager)
> {
>      return pmanager->GetDevice()->GetChannel(0)->GetLowPassFilter()->GetCodecName();
> }
>
> That's the kind of lines I was thinking along.
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals