1. Problem It Solves
decltype reports the type of an expression without evaluating it, and a trailing return type lets a function name parameter-dependent result types. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 5, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: decltype reports the type of an expression without evaluating it, and a trailing return type lets a function name parameter-dependent result types. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
auto add(T a, U b) -> decltype(a + b);5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
decltype(name)anddecltype((name))can produce different types because the parenthesized form examines the expression category.
7. When to Use It
Use it when a return type depends on an expression involving function parameters.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
A small add function returns the exact type of a + b; adding an int and a double produces a double result. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/06_decltype_trailing_return_type/main.cpp
#include <iostream>
auto add(int left, double right) -> decltype(left + right) {
return left + right;
}
int main() {
int count = 4;
decltype(count) copy = 6; // exactly int
const auto result = add(count, 2.5);
std::cout << "copy=" << copy << '\n';
std::cout << "result=" << result << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — In
auto add(int a, double b) -> decltype(a + b), what dodecltypeand the trailing return type do?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation:
decltype(name)anddecltype((name))can produce different types because the parenthesized form examines the expression category. What is the smallest C++11-safe correction?