C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 20 Oct 2022 18:32:34 +0200
Longer "equivalent" code (but handles several indirections). Actual example in the main function:   #include<exception> #include<iostream> usingnamespace std; struct S {     int i;     int getValue() { return i; } }; template<class C> struct Pointer {     C* p;     C* operator->() {         if (p)             return p;         else             throw exception();     } }; void safe(auto lambda) {     try {         lambda();     } catch (const exception& e) {} } int main() {     Pointer<S> ptr{nullptr};     S s2{2};     Pointer<S> ptr2{&s2};     safe([&]{ cout << ptr->getValue(); });  // does not print     safe([&] { cout << ptr2->getValue(); }); // prints "2"     return0; }     -----Ursprüngliche Nachricht----- Von:Richard Hodges via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 20.10.2022 13:01 Betreff:Re: [std-proposals] The null-aware arrow operator: ?-> An:std-proposals_at_[hidden]; CC:Richard Hodges <hodges.r_at_[hidden]>;   On Thu, 20 Oct 2022 at 12:25, Peter Olsson via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote: If you don't use a colon (:) does that mean the type of the ?-> expression is always void?  You can't write the following, can you?     std::cout << ptr?->get_value();  These are equivalent:   ptr ? std::cout << ptr->get_value(), void() : void();  ptr ?  std::cout << ptr->get_value()   : std::cout;  if (ptr) std::cout << ptr->get_value();  The "optional operator" is unnecessary syntactic sugar found in a couple of populist languages.   >The three-way comparison operator <=> is 3 chars long; ?-> now seems reasonable IMHO.  There are also <<= and >>= so I don't think the length should be controversial. -- Std-Proposals mailing list Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-10-20 16:32:36