Date: Sun, 8 Feb 2026 00:55:58 +0000
For a few years now people have been talking about bringing guaranteed
NRVO into C++ so that we can return an immovable-and-uncopiable type
by value from a function after having altered the variable, for
example:
mutex Func(void)
{
mutex m;
m.lock();
return m;
}
Sebastian and I were writing a paper ( https://wg21.link/p3357 ) which
would lend to code such as:
mutex Func(void)
{
return prvalue<mutex>( [](mutex &m){ m.lock(); } );
}
There's also a very long paper by Anton: https://wg21.link/p2025
To achieve NRVO in C++, I think we need a happy medium between three things:
(1) Good enough functionality and convenience for programmers
(2) Not too much hassle for compiler writers
(3) Not too much complication for standards writers
I think the happy medium is to mark the return slot as follows:
mutex Func(void)
{
[[nrvo]] mutex m;
m.lock();
return m;
}
and to only allow one variable marked 'nrvo' in any scope. So the
following is disallowed:
mutex Func(void)
{
[[nrvo]] mutex m2;
[[nrvo]] mutex m;
m.lock();
return m;
}
but the following is allowed:
mutex Func(void)
{
{
[[nrvo]] mutex m;
m.lock();
}
[[nrvo]] mutex m;
m.lock();
return m;
}
Furthermore, if there is an 'nrvo' variable in scope, then the return
statement must return said variable, so the following is disallowed:
mutex Func(void)
{
[[nrvo]] mutex m;
m.lock();
mutex m2;
return m2;
}
I've written this into the GNU g++ compiler, you can test it here:
https://godbolt.org/z/5oe34M731
In the above GodBolt you need to use [[gnu::nrvo]] instead of [[nrvo]]
but I'll fix that on Monday.
Here's the compiler patch:
https://github.com/healytpk/gcc-thomas-healy/commit/tag_nrvo
NRVO into C++ so that we can return an immovable-and-uncopiable type
by value from a function after having altered the variable, for
example:
mutex Func(void)
{
mutex m;
m.lock();
return m;
}
Sebastian and I were writing a paper ( https://wg21.link/p3357 ) which
would lend to code such as:
mutex Func(void)
{
return prvalue<mutex>( [](mutex &m){ m.lock(); } );
}
There's also a very long paper by Anton: https://wg21.link/p2025
To achieve NRVO in C++, I think we need a happy medium between three things:
(1) Good enough functionality and convenience for programmers
(2) Not too much hassle for compiler writers
(3) Not too much complication for standards writers
I think the happy medium is to mark the return slot as follows:
mutex Func(void)
{
[[nrvo]] mutex m;
m.lock();
return m;
}
and to only allow one variable marked 'nrvo' in any scope. So the
following is disallowed:
mutex Func(void)
{
[[nrvo]] mutex m2;
[[nrvo]] mutex m;
m.lock();
return m;
}
but the following is allowed:
mutex Func(void)
{
{
[[nrvo]] mutex m;
m.lock();
}
[[nrvo]] mutex m;
m.lock();
return m;
}
Furthermore, if there is an 'nrvo' variable in scope, then the return
statement must return said variable, so the following is disallowed:
mutex Func(void)
{
[[nrvo]] mutex m;
m.lock();
mutex m2;
return m2;
}
I've written this into the GNU g++ compiler, you can test it here:
https://godbolt.org/z/5oe34M731
In the above GodBolt you need to use [[gnu::nrvo]] instead of [[nrvo]]
but I'll fix that on Monday.
Here's the compiler patch:
https://github.com/healytpk/gcc-thomas-healy/commit/tag_nrvo
Received on 2026-02-08 00:54:35
