Date: Sun, 19 May 2024 21:43:28 +0100
On Sun, May 19, 2024 at 9:28 PM Frederick Virchanza Gotham wrote:
>
> A less-ambitious alternative would be to
> allow something like:
>
> mutex Func(void)
> {
> auto &m = _Retvar{};
> m.lock();
> return _Retvar;
> }
>
> I propose that the operator "_Retvar" can only be used in a function
> that returns by value. Its job is to create an object of the return
> type, and it returns a reference to the new object. Then later you
> just write "return _Retvar" to return from the function.
Furthermore, to make it easier for the people writing the Standard,
and to make it easier for compiler vendors, just dictate that it's
undefined behaviour to use _Retvar incorrectly. Here are a few
examples of incorrect usage:
----- Incorrect Usage No. 1:
mutex Func(void)
{
auto &m = _Retvar{};
m.lock();
return mutex(); // oops, can't do this
}
----- Incorrect Usage No. 2:
mutex Func(int const a)
{
auto &m = _Retvar{};
auto &m2 = _Retvar{}; // oops, can't do this
m.lock();
return _Retvar;
}
The job of the standard-writer and the compiler-writer is made a lot
easier by stipulating that incorrect use of _Retvar results in
undefined behaviour.
>
> A less-ambitious alternative would be to
> allow something like:
>
> mutex Func(void)
> {
> auto &m = _Retvar{};
> m.lock();
> return _Retvar;
> }
>
> I propose that the operator "_Retvar" can only be used in a function
> that returns by value. Its job is to create an object of the return
> type, and it returns a reference to the new object. Then later you
> just write "return _Retvar" to return from the function.
Furthermore, to make it easier for the people writing the Standard,
and to make it easier for compiler vendors, just dictate that it's
undefined behaviour to use _Retvar incorrectly. Here are a few
examples of incorrect usage:
----- Incorrect Usage No. 1:
mutex Func(void)
{
auto &m = _Retvar{};
m.lock();
return mutex(); // oops, can't do this
}
----- Incorrect Usage No. 2:
mutex Func(int const a)
{
auto &m = _Retvar{};
auto &m2 = _Retvar{}; // oops, can't do this
m.lock();
return _Retvar;
}
The job of the standard-writer and the compiler-writer is made a lot
easier by stipulating that incorrect use of _Retvar results in
undefined behaviour.
Received on 2024-05-19 20:43:38