C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Wed, 22 May 2024 19:07:19 +0000

>> But as I have explained there cannot be a “getMutexTheOtherTeamToldMeToUse()” because it is a std::mutex and it must be initialized at the address provided by the caller.
> Exactly! It doesn't work because the language doesn't allow it to work. If the language allowed it to work, then it would work!
> By your reasoning, this should also not work:
> std::mutex getMutex() { return std::mutex(); }

No, that is not what I meant, at all. Guaranteed RVO is guaranteed since C++17 if my memory serves me correctly.
What I mean is that you would never write this code. In no circumstance you would rely on NRVO to return a mutex by value as opposed to passing an already initialized mutex by reference.

There can not have been a different std::mutex created by “getMutexTheOtherTeamToldMeToUse()”, since its lifetime could only have started after the call was made and the only mutex that ever exists is the one that is created in the memory space made available by the caller, so they couldn’t have possibly have any other available.
And given its design there’s absolutely no reason why you wouldn’t pass an already initialized mutex to the function making NRVO irrelevant.

There may be other classes where you would want to do this, std::mutex is not one of them.

Received on 2024-05-22 19:07:22