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@lists.isocpp.org>
Gesendet: Do 20.10.2022 13:01
Betreff: Re: [std-proposals] The null-aware arrow operator: ?->
An: std-proposals@lists.isocpp.org;
CC: Richard Hodges <hodges.r@gmail.com>;
 

On Thu, 20 Oct 2022 at 12:25, Peter Olsson via Std-Proposals <std-proposals@lists.isocpp.org> 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@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
-- 
 Std-Proposals mailing list
 Std-Proposals@lists.isocpp.org
 https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals