1. Problem It Solves
A function return type can be long or dependent on an expression. C++14 allows a normal function to use auto as its return placeholder, letting the compiler deduce the type from its return statement.
Focus on the smallest useful form, its observable behavior, and its safety boundary.
2. Prerequisites
Days 2 and 9:
autodeduction, expression types, function returns, and generic lambdas.
3. Core Idea
The function still has one static return type. The compiler examines all reachable value-returning statements and requires them to deduce the same type; this is compile-time deduction, not runtime variation.
Identify the objects and types, today's operation, and the printed result. This connects syntax to behavior.
4. Minimal Syntax
auto square(int value) {
return value * value;
}5. How It Works
The compiler determines the type of the return expression after ordinary arithmetic conversions.
That deduced type becomes the function's fixed return type for declarations and calls.
The integer function returns an integer, while a separate function using double arithmetic returns a double.
6. Common Mistakes
Returning
inton one branch anddoubleon another does not automatically choose a common type; deduction fails.Do not copy the pattern without checking every return expression, implicit conversions inside those expressions, and whether a reference is intended. 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 expression makes the result type obvious and manually repeating it would be fragile.
Avoid it when an explicit return type better documents an API contract or accidental type changes must be prevented.
8. Simple Example
The sample defines square for integers and half for doubles. Each body contains one clear return expression, making its deduced type easy to inspect.
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/12_function_return_type_deduction_auto/main.cpp
#include <iostream>
auto square(int value) {
return value * value; // deduced as int
}
auto half(double value) {
return value / 2.0; // deduced as double
}
int main() {
std::cout << "square: " << square(6) << "\n";
std::cout << "half: " << half(9.0) << "\n";
}
9. Key Takeaways
Return-type deduction removes spelling, but it does not remove the single-type requirement.
The function still has one static return type. The compiler examines all reachable value-returning statements and requires them to deduce the same type; this is compile-time deduction, not runtime variation.
The compiler or library follows a precise rule; verify every return expression, implicit conversions inside those expressions, and whether a reference is intended.
Prefer the smallest form that communicates intent and measure costs when performance matters.
10. Self-Check Questions
Easy — What is the main purpose of Function Return Type Deduction with auto?
Medium — What return types are deduced for
square(int)andhalf(double)?Hard — Why does a function with
return 1;andreturn 2.0;on different branches failautoreturn deduction?