Date: Thu, 31 Oct 2024 12:01:18 +0200
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.
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.
Received on 2024-10-31 10:01:24