1. Problem It Solves
Generic code often needs a plain value copy with reference and top-level const removed. C++23 lets auto appear as a cast-like expression for that explicit decay-copy. The goal is to make the relevant rule visible in code instead of relying on an assumption about what “C++23 support” means.
2. Prerequisites
Day 3: references, value categories, and type deduction.
Day 8: reading expressions evaluated in different contexts.
3. Core Idea
Treat auto(x) as putting x through the same value-deduction filter used by an auto variable, then producing a fresh prvalue result. Read the syntax from left to right, identify the value, object, or range being transformed, and then check its resulting type, lifetime, or ownership. Standardization and implementation are separate: a C++23 mode may still lack one library component, so a feature-test macro is part of responsible portable use.
4. Minimal Syntax
auto copy = auto(source);
auto copy2 = auto{source};5. How It Works
The sample builds the smallest expression or object needed for Decay-Copy with
auto(x)andauto{x}.It copies a
const int&through both spellings and checks the deduced types. The compiler and library apply the relevant rule before the program observes the result.The program prints or checks two independent
intvalues whose changes do not modify the source, making the important behavior easy to verify.
6. Common Mistakes
Expecting
auto{x}to preserve a reference or using it where copying an expensive object is accidental causes logic or performance surprises.Enabling C++23 mode without checking the relevant feature macro may select code that the installed compiler or standard library does not implement yet.
7. When to Use It
Use it when the task involves generic expressions that deliberately need a decayed value rather than an alias.
Avoid it when the task involves large objects when retaining a reference or moving is the intended ownership behavior.
8. Simple Example
A wrapper snapshots a configuration value so later updates to the original reference cannot affect the current operation. The downloadable program keeps the data fixed so the output can be compared without entering input.
Complete sample code
Source file
cpp23/09_auto_decay_copy/main.cpp
#include <iostream>
#include <type_traits>
int main() {
const int original = 7;
const int& source = original;
auto first = auto(source); // C++23 decay-copy expression
auto second = auto{source};
static_assert(std::is_same_v<decltype(first), int>);
static_assert(std::is_same_v<decltype(second), int>);
first = 10;
second = 20;
std::cout << original << ' ' << first << ' ' << second << '\n';
}
9. Key Takeaways
Separate the language rule from compiler and library availability.
Keep lifetime, ownership, and deduced types visible when they affect correctness.
Prefer the smallest syntax that communicates the intent.
Guard facilities that are not yet uniformly implemented.
10. Self-Check Questions
Easy — What is the smallest syntax in Section 4, and what main job does it perform?
Medium — Read the sample program: which value, type, or branch is observed, and why?
Hard — For
const int& r, what aredecltype(auto(r))and the value category of the expressionauto(r)?