C++ Logo


Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 20 Oct 2022 09:33:37 +0200
Hi Stephane,   the ternary operator ? has the current disadvantage that it cannot be customized. Customization of ?-> could be useful for smart pointers and iterators, when the test should do more than test for false the pointer converted into bool.   Even if a 3-argument operator? or operator?-> would be introduced, with current function call semantics, all parameters are evaluated before the function call, which would nullify the check for nullptr.   Similarly && and || cannot be customized fully.   Perhaps there are ideas for partial evaluations of function parameters in general (e.g. "lambda parameters", which can be evaluated in a delayed manner by the called function (internally implementable as callback) or some fixed ways for certain operators like operator?, operator??, operator||, operator?->.   Sebastian   -----Ursprüngliche Nachricht----- Von:Stéphane Duguay via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 20.10.2022 05:07 Betreff:[std-proposals] The null-aware arrow operator: ?-> An:std-proposals_at_[hidden]; CC:Stéphane Duguay <s_at_[hidden]>; 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 -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-10-20 07:33:39