ELSEIF
Your brief EB
506 stories from 219 feeds 1271 clusters Refreshed 30 minutes ago next pull 06:42

LANGUAGES Signal 142

C++23 enables implicit move for rvalue reference returns without std::move

C++23 removes the need for explicit std::move in certain rvalue reference return cases, improving performance by default

WHY IT MATTERS

This change reduces boilerplate and potential errors when returning rvalue references in C++. Engineers can now rely on the compiler to handle moves implicitly, aligning with modern C++ best practices for performance optimization.

Written by elseif from the cluster below · every claim links back to a source

The three things worth knowing

01

C++23 introduces implicit move for rvalue reference returns, eliminating manual std::move in specific cases

02

The change applies to functions returning rvalue references or objects bound to rvalue references

03

This aligns with the broader C++ goal of minimizing unnecessary copies and moves while reducing code verbosity

THE READ

What the cluster adds up to.

ORIGINAL ANALYSIS

C++23 closes a long-standing gap in the language's move semantics by making implicit moves work for rvalue reference returns. Previously, functions returning an rvalue reference parameter required explicit std::move to avoid copies, creating inconsistency with other return-value optimizations. The new rule means code like `return val;` in a function taking `Apple&& val` will now perform a move rather than a copy, matching the behavior developers intuitively expect.

The change primarily affects two scenarios: functions returning an rvalue reference parameter directly, and functions returning an rvalue reference type. In both cases, the compiler now generates move operations automatically. This eliminates the need for manual std::move in these contexts, reducing boilerplate while maintaining performance. The improvement is particularly noticeable in generic code where type traits might otherwise force explicit moves.

While this is a welcome simplification, it doesn't eliminate all cases where std::move remains necessary. The change only applies to rvalue reference returns - other scenarios like conditional returns or returning members still require explicit moves. Engineers should note that this is a C++23 feature, meaning adoption depends on compiler support and project migration timelines. The change also doesn't affect NRVO, which remains non-guaranteed in many cases.

The practical impact is twofold: first, it reduces cognitive load by making move semantics more consistent across different return patterns. Second, it prevents subtle performance bugs where developers might forget std::move in these specific cases. However, teams will need to update their style guides and static analysis rules to account for the new implicit behavior, particularly in codebases that previously enforced explicit std::move in all return contexts.

Written by elseif from the cluster below · checked for specifics the sources never contained

THE CLUSTER

Same story, 1 feed.

ORDERED BY FIRST SEEN
andreasfertig.com via Hacker News Move in C++ without a std:move Open ↗