optional<Type> func() { return Type{}; }

Type t = *func(); //trigger move of contained object as operator* is aware of r-refness of this and moves value out…

 

See: https://en.cppreference.com/w/cpp/utility/optional/operator*

 

From: Std-Proposals <std-proposals-bounces@lists.isocpp.org> On Behalf Of jianping z via Std-Proposals
Sent: Monday, December 16, 2019 10:07 PM
To: Barry Revzin <barry.revzin@gmail.com>; std-proposals@lists.isocpp.org
Cc: jianping z <zjpwork@gmail.com>
Subject: Re: [std-proposals] proposal: new const-able feature ( use "const?" , or new "constable" keyword) for method/function

 

also, for your example, do we really need following 2 member methods returning value of  type "T&&" and "const T&&"?

    auto operator*() &&      -> T&&       { return std::move(value); }
    auto operator*() const&& -> T const&& { return std::move(value); }

do you have code example to show the possible usage of these 2 methods?

Best Regards,