In modern C++, returning const values from functions can silently disable move semantics, causing the compiler to fall back to copying instead of moving, which significantly degrades performance; this occurs because move constructors require non-const rvalue references to steal resources, and const objects cannot bind to non-const references, making move optimization unavailable.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
This const Might Disable Move SemanticsAdded:
Take a look at this function on the screen. At first glance, it seems completely fine. In fact, this kind of code is very common in many C++ projects. Why? Because many people think returning const is safer. It prevents others from arbitrarily modifying the return value. And they even believe that the more const you add, the more professional the code looks. Sounds reasonable, right? But in modern C++, this seemingly harmless const can actually make your code slower. So, why is that? Take a look at this piece of code. Under normal circumstances, modern C++ tries to avoid unnecessary copies as much as possible. If possible, the compiler will directly move this temporary object instead of copying it.
And the overhead of moving is usually much smaller than copying. But here's the problem. The move constructor typically looks like this. It takes a non-const rvalue reference. Why must it be non-const? Because the essence of move is actually stealing resources.
After the move, the source object is usually modified. Its internal resources are transferred away. For example, pointers might be set to null, lengths might become zero. But now, look at this function again. It returns a const temporary object. And a const object cannot bind to a non-const rvalue reference. What happens as a result? The move constructor may become effectively unavailable. So, the compiler falls back to using copy. In other words, you thought you were writing safer code, but you've quietly disabled move optimization. The code ends up slower.
Therefore, in modern C++, it's generally not recommended to return const value.
Because one of the core ideas of modern C++ is not to prevent compiler optimizations. Const correctness is important. But mindlessly adding const everywhere does not equal modern C++.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsβ’2026-05-28
How agent o11y differs from traditional o11y β Phil Hetzel, Braintrust
aiDotEngineer
450 viewsβ’2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanationπ―β
LearnwithSahera
1K viewsβ’2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsβ’2026-05-29
Search Algorithms Explained in 60 Seconds! π€π¨
samarthtuliofficial
218 viewsβ’2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsβ’2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsβ’2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsβ’2026-06-01











