C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Copy-construct, move-construct, and PR-construct

From: Breno Guimarães <brenorg_at_[hidden]>
Date: Wed, 23 Aug 2023 13:01:25 -0300
Perhaps a fair addition to the language is to allow RVO with slicing iff
sizeof(Base) == sizeof(Derived):

struct LockedMutex : public std::mutex {
    LockedMutex() { lock(); }
};

std::mutex getLockedMutex() { return LockedMutex(); } // Hey, why not?

On the other hand, that code could be a bug more often than not.

On Wed, Aug 23, 2023 at 12:50 PM Arthur O'Dwyer via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, Aug 23, 2023 at 7:47 AM Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
> wrote:
>
>> On Wed, Aug 23, 2023 at 7:39 AM Frederick Virchanza Gotham via
>> Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>>> On Wed, Aug 23, 2023 at 4:10 PM Ville Voutilainen <
>>> ville.voutilainen_at_[hidden]> wrote:
>>> >
>>> > I wonder what happened to the third strategy, which is to pass as the
>>> > argument of existing emplace an object
>>> > that will perform the function invocation in its conversion operator
>>> > to the optional's element type, which then
>>> > allows doing this without any library or language changes, and has
>>> > field experience as a solution to this problem.
>>>
>>>
>>> Do you mean something like the following?
>>> GodBolt: https://godbolt.org/z/PfM1EKaWo
>>>
>>
>> Yes, but it can be done much simpler.
>> https://godbolt.org/z/Gr9sT6Ga9
>>
>> #include <optional>
>> #include <mutex>
>>
>> template<class F>
>> struct SCSE {
>> F f_;
>> operator auto() const { return f_(); }
>> };
>>
>> int main(void) {
>> std::optional<std::mutex> om;
>> om.emplace(SCSE([] {
>> return std::mutex();
>> }));
>> }
>>
>> The deficiency with the current state of the world is that even SCSE
>> doesn't help at all with returning immobile objects; you can't use SCSE to
>> return a locked mutex, for example.
>>
>
> ...and of course you don't need the SCSE at all, if you have a constructor
> that already does what you want.
>
> https://godbolt.org/z/fWz3EzWqx
>
> int main(void) {
> std::optional<std::mutex> om;
> om.emplace();
> }
>
> The reason to use the SCSE is if you have a constructor that's non-public
> (so `optional::emplace` can't see it), or if you have a
> factory-function-that's-not-a-constructor (so `optional::emplace` isn't set
> up to use it).
>
> –Arthur
>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-08-23 16:01:38