1. Problem It Solves
Long or dependent types make declarations noisy and easy to duplicate incorrectly. auto derives a new variable type from an initializer, while decltype asks for the type of a name or expression.
Focus on the smallest useful form, its observable behavior, and its safety boundary.
2. Prerequisites
Day 1 toolchain; variables,
const, references, and basic expressions from C++11.
3. Core Idea
Use auto like template argument deduction: top-level const and references are normally dropped. Use decltype when the exact declared type or value category must be preserved.
Identify the objects and types, today's operation, and the printed result. This connects syntax to behavior.
4. Minimal Syntax
const int n = 7;
auto copy = n; // int
decltype(n) exact = n; // const int5. How It Works
The initializer for an
autodeclaration is examined and deduction forms a fresh variable type.For
decltype(name), the declared type of the name is returned; parentheses can instead make expression-category rules apply.The copied value can change independently, while a deduced reference changes the original object.
6. Common Mistakes
Assuming plain
autoalways preservesconstor&can turn an intended alias into a copy.Do not copy the pattern without checking whether deduction creates a value, a reference, or a const-qualified type. A program may compile while still having the wrong lifetime, ownership, invalidation, ordering, or performance behavior.
7. When to Use It
Use it when the type is obvious from the initializer or depends on an expression and spelling it manually adds noise.
Avoid it when the inferred type hides an important conversion, ownership decision, or precision loss.
8. Simple Example
A configuration value is copied with auto, while decltype(ref) preserves a reference to the live setting. Printing both values reveals which declaration owns a separate value.
The .cpp file uses fixed data. Predict its output, compile it, then change one value and test the prediction.
Complete sample code
Source file
cpp14/02_auto_decltype_type_deduction_review/main.cpp
#include <iostream>
int main() {
const int base = 7;
auto copy = base; // int: top-level const is dropped
decltype(base) exact = base; // const int
int value = 10;
int& ref = value;
decltype(ref) alias = value; // int&
copy = 8;
alias = 42;
std::cout << "copy: " << copy << "\n";
std::cout << "exact: " << exact << "\n";
std::cout << "value: " << value << "\n";
}
9. Key Takeaways
Type deduction removes repetition, but the programmer must still reason about qualifiers and references.
Use
autolike template argument deduction: top-levelconstand references are normally dropped. Usedecltypewhen the exact declared type or value category must be preserved.The compiler or library follows a precise rule; verify whether deduction creates a value, a reference, or a const-qualified type.
Prefer the smallest form that communicates intent and measure costs when performance matters.
10. Self-Check Questions
Easy — What is the main purpose of Reviewing auto, decltype, and Type Deduction?
Medium — After
const int n = 7; auto x = n;, may the program assignx = 8, and why?Hard — Compare
decltype(value)withdecltype((value))for a namedintvariable. Which one is a reference?