https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87057
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Konstantin Kharlamov from comment #2) > (In reply to Jonathan Wakely from comment #1) > > That would require a lot of special-casing just for std::variant. > > Well, I think, in place of std::variant there could be any struct-wrapper; If you have an example with a simple struct then please show it, maybe we can improve that case. std::variant is hundreds of lines of complex metaprogramming, not a struct-wrapper. > > N.B. If you just write "return ret;" it will compile fine. In general > > "return x;" is better than "return {x};" because it doesn't prevent NRVO. > > Thanks, I prefer the `{x}` to just `x` because in the latter I'm being > explicit that the `x` is not the type I'm returning, but there's some other > type that it's being wrapped to. Which means you pessimize the code, because you force a copy, not an elidable move. > As far as such trivial optimizations concerned, I'd prefer to rely on the > compiler figuring that out — in the end, that's why we don't write assembly, > right? =) If you write "return ret;" the compiler is allowed to move from the variable, and is allowed to use the NVRO. It's not allowed to do that if you write "return {ret};".