decltype
decltype(expr) is the type of expr, including references and const, without evaluating expr. Use it when auto's drop-ref rules would throw away the part of the type you need.
Detailed Explanation
auto answers "what would a function template parameter be if I passed this expression". That answer strips references and top-level const. Sometimes you need the type the expression actually has. decltype is that query.
There are two rules, and they are not the same.
If the argument is an unparenthesised name (decltype(x), decltype(v.size) is wrong, decltype(v)), you get the declared type of that name. int x; → int. int& r = x; → int&. const int n = 1; → const int. No extra reference is added.
If the argument is any other expression, the value category decides:
Value category of expr | decltype(expr) |
|---|---|
| prvalue (temporary, literal) | T |
lvalue (a named object used as an expression, v[0], ++x) | T& |
xvalue (std::move(x), a+b for some overloads) | T&& |
Parentheses change the first rule into the second. decltype(x) is the declared type. decltype((x)) treats x as an lvalue expression, so it is T& if x is a non-ref T. That extra pair of parentheses is a famous trap in generic code.
decltype does not evaluate the expression. decltype(f()) does not call f. It only looks at the signature and the form of the expression. That is why you can write decltype(v[0]) in a template even on a path that never indexes.
The classic use is a trailing return type in C++11: auto add(A a, B b) -> decltype(a + b). C++14 auto return deduction covers many of those cases, but not when you need to name the type before the body, or when you need a reference preserved. decltype(auto) as a return type or a variable type applies decltype rules to the initialiser or return operand. It is how a forwarding function returns T& when it called something that returned T&, and T when it called something that returned T.
In templates, decltype plus std::declval lets you ask "what would t.foo() return if I had a T" without constructing a T: decltype(std::declval<T>().foo()). Concepts and requires expressions have taken some of that work in C++20, but decltype is still how you name the resulting type.
decltype is also how you declare a variable with the same type as another when you do not want a copy: decltype(x) y = x; if x is a reference, y is a reference and must bind to something.
Do not sprinkle decltype on every local. If auto already gives the type you mean, use auto. Reach for decltype when the reference or const is the point, or when you are computing a type from an expression in a template.
Code Examples
#include <iostream>
#include <type_traits>
#include <utility>
int main() {
int x = 7;
int& r = x;
static_assert(std::is_same_v<decltype(x), int>);
static_assert(std::is_same_v<decltype(r), int&>);
static_assert(std::is_same_v<decltype((x)), int&>); // extra parens: lvalue
decltype(r) y = x; // y is int&
y = 8;
std::cout << x << '\n'; // prints: 8
}#include <iostream>
#include <string>
#include <utility>
template <typename F, typename T>
decltype(auto) apply(F&& f, T&& value) {
// Returns whatever f returns: value, lvalue ref, or rvalue ref.
return std::forward<F>(f)(std::forward<T>(value));
}
int main() {
auto doubled = apply([](int n) { return n * 2; }, 21);
std::cout << doubled << '\n'; // prints: 42
std::string name = "ada";
auto& alias = apply([](std::string& s) -> std::string& { return s; }, name);
alias = "al";
std::cout << name << '\n'; // prints: al
}Common Gotchas
Parentheses change the result
`decltype(x)` and `decltype((x))` are different when `x` is not already a reference. In a macro or a `decltype(auto)` return, an extra pair of parens can turn a value return into a dangling reference.
`decltype(auto) x = expr;` keeps a reference to a temporary
if `expr` is an lvalue that dies at the semicolon, you have a dangling reference and no compiler error.
`decltype` is not `typeof`
GCC's `typeof` is an extension and evaluates differently. Portable code uses `decltype`.
`decltype(a + b)` can be a reference
if `operator+` returns `T&` (unusual, but legal), `decltype` reports `T&`. Do not assume arithmetic yields a prvalue.