auto vs. decltype vs. decltype(auto)?
C++Level: IntermediateModern C++ Features
auto vs. decltype vs. decltype(auto)?
Hints
- Identify types, object lifetimes and compile-time decisions.
- Separate language guarantees from a particular compiler's behavior.
Answer
Auto Type Deduction
The auto keyword follows the same deduction rules as template value
parameters, with one notable exception: braced initializers. This example
demonstrates how auto deduces types for different initializers, including
the special behavior of auto&& as a universal reference. Understanding
these rules is essential for writing modern C++ code that leverages type
inference effectively:
int main() {
auto x = 123; // int
const auto cx = x; // const int
const auto& rx = x; // const int&
auto&& urx = x; // int& (x is lvalue)
auto&& urcx = cx; // const int& (cx is lvalue)
auto&& urrx = rx; // const int& (rx is lvalue)
auto&& urrv = 12; // int&& (12 is rvalue)
}
decltype(auto) Type Deduction
Unlike auto, decltype(auto) preserves the exact type including
references and cv-qualifiers. This distinction is critical when the original
type information must be retained, such as when forwarding return values
from wrapped functions. While auto applies template argument deduction
rules (which strip references), decltype(auto) applies decltype
semantics to the initializer expression.
The first example contrasts decltype(auto) with auto. Note how
auto strips the reference and const from crx, producing a plain int,
while decltype(auto) preserves the full const int& type. Similarly,
decltype(auto) preserves rvalue references, which auto would decay
to a value type:
#include <type_traits>
int main(int argc, char *argv[]) {
int x = 0;
const int cx = x;
const int& crx = x;
int&& z = 0;
// decltype(auto) preserves cv-qualifiers and references
decltype(auto) y1 = crx;
static_assert(std::is_same_v<const int&, decltype(y1)>);
// auto strips cv-qualifiers and references
auto y2 = crx;
static_assert(std::is_same_v<int, decltype(y2)>);
// decltype(auto) preserves rvalue references
decltype(auto) z1 = std::move(z);
static_assert(std::is_same_v<int&&, decltype(z1)>);
}
Application in return type deduction:
This behavior is particularly useful for generic functions that must preserve
the exact return type of an expression. In this example, foo uses auto
return type deduction, which strips the reference and returns by value. In
contrast, bar uses decltype(auto), preserving the const int& return
type. This distinction matters for performance (avoiding copies) and semantics
(maintaining reference semantics):
#include <type_traits>
auto foo(const int& x) {
return x; // Returns int (reference stripped)
}
decltype(auto) bar(const int& x) {
return x; // Returns const int& (reference preserved)
}
int main(int argc, char *argv[]) {
static_assert(std::is_same_v<int, decltype(foo(1))>);
static_assert(std::is_same_v<const int&, decltype(bar(1))>);
}
Source and licence: C/C++ Interview Cheatsheet — Chang Ning Tsai · MIT