C++ Logo

std-proposals

Advanced search

[std-proposals] The null-aware arrow operator: ?->

From: Stéphane Duguay <s_at_[hidden]>
Date: Wed, 19 Oct 2022 23:07:16 -0400
The null-aware arrow operator: ?->

Instead of:
    if(ptr)
        ptr->func();
Write:
    ptr?->func();

The very bad:
    if( ptr && ptr->getSub() )
        ptr->getSub()->func();
Becomes:
    ptr?->getSub()?->func();

Support for the "default value if ptr is null" use case:
    n = ptr?->get_value() : 42;
Instead of:
    n = ptr ? ptr->get_value() : 42;

The three-way comparison operator <=> is 3 chars long; ?-> now seems
reasonable IMHO.

I'm unsure of all the possibilities for null-aware operators in C++,
but there's probably more to explore. I couldn't find anything in
relation to "null-aware" on the mailing list archive. "Null safety"
itself is a wider concept than null-awareness. If this was already
posted, sorry!

-------
s|d

Received on 2022-10-20 03:07:28