English

C++ type deduction

9/1/2026
28
C++ type deduction

Prerequisites

Value category

In C++ every expression has a value category, and there are three value categories.

  • lvalue — has identity, cannot be moved from (implicitly). Typical examples: named variables, function parameters, string literals.
  • xvalue — has identity, can be moved from. Typical examples: a function call returning an rvalue reference, std::move(obj), member access on an xvalue.
  • prvalue — no identity, can be moved from. Typical examples: literals like 42 and true, a function returning by value (non-reference return), arithmetic expressions like a + b.
Note: "literals are prvalues" has one exception — a string literal is an lvalue. decltype("hello") is const char(&)[6], because the literal is a real array object with static storage duration.

Based on whether it has identity

  • glvalue (generalized lvalue) — has identity, i.e. it names a distinct object you can refer to again. Includes lvalue and xvalue.
  • prvalue

Careful: identity does not mean you can apply &. The built-in address-of operator requires an lvalue specifically, so taking the address of an xvalue is ill-formed:

C++
int x = 10;

int* p1 = &x;              // ok, x is an lvalue
int* p2 = &std::move(x);   // error: taking address of rvalue

Based on whether it can be moved from

  • lvalue — cannot be moved from
  • rvalue — can be moved from; includes xvalue and prvalue

Reference binding rules

lvalue reference binding rules

  1. A non-const lvalue reference (T&) binds only to an lvalue; it cannot bind to an xvalue or a prvalue.
  2. A const lvalue reference (const T&) binds to an lvalue, an xvalue or a prvalue.
C++
int val = 42;
int& r1 = val;             // ok

int& r2 = std::move(val);  // error: xvalue can not bind to T&
int& r3 = r1 + 1;          // error: prvalue can not bind to T&

// const lvalue reference
const int& cr1 = val;              // ok, lvalue
const int& cr2 = std::move(val);   // ok, xvalue
const int& cr3 = cr1 + 1;          // ok, prvalue (its lifetime is extended)

rvalue reference binding rules

T&& binds only to an xvalue or a prvalue.

C++
int val = 42;
int&& r1 = std::move(val);   // ok, xvalue
int&& r2 = r1 + 1;           // ok, prvalue

Note that r1 itself is an lvalue — it is a named variable. Its type is int&&, but the expression r1 has identity, which is why r1 + 1 above is a prvalue rather than an xvalue.

const T&& is syntactically valid but rarely useful for moving: it binds only to rvalues, yet you cannot mutate what it refers to, which defeats the purpose of an rvalue reference. It does have one idiomatic use though — deleting the rvalue overload so a dangling reference cannot be formed. The standard library does exactly this:

C++
template<class T> void ref(const T&&)      = delete;   // std::ref
template<class T> void cref(const T&&)     = delete;   // std::cref
template<class T> void as_const(const T&&) = delete;   // std::as_const

Type deduction has three cases

  1. Function template argument deduction
  2. auto deduction
  3. decltype deduction

1. Function template argument deduction

C++
template<typename T>
void func(ParamType param);

func(expr);

T and ParamType are deduced from expr, and which of the three cases applies depends on the shape of ParamType.

Case A: ParamType is a reference or pointer (but not a forwarding reference)

C++
template<typename T>
void func(T& param);
  • expr is an lvalue: T is expr's type, with cv-qualifiers kept.
  • expr is an rvalue: compile error — an rvalue cannot bind to a non-const lvalue reference.
C++
int x = 10;
const int cx = x;

func(x);    // T = int,       param = int&
func(cx);   // T = const int, param = const int&

The cv-qualifiers are kept, which is the whole point of this case.

The "compile error on rvalue" rule applies to a non-const T&. If ParamType is const T&, rvalues bind perfectly well, and the const moves out of T because it is already in ParamType:

C++
template<typename T>
void func(const T& param);

func(x);    // T = int, param = const int&
func(cx);   // T = int, param = const int&
func(27);   // T = int, param = const int&   — an rvalue is fine here

Pointers behave the same way:

C++
template<typename T>
void func(T* param);

const int* px = &x;

func(&x);   // T = int,       param = int*
func(px);   // T = const int, param = const int*

Case B: ParamType is a forwarding reference (universal reference) T&&

A forwarding reference has special rules:

  • expr is an lvalue of type A: T is deduced as A&; after reference collapsing ParamType is A&.
  • expr is an rvalue of type A: T is deduced as A; after reference collapsing ParamType is A&&.

This is the only case where T is ever deduced as a reference type.

C++
template<typename T>
void func(T&& param);

int v = 0;

func(v);              // lvalue:  T = int&, int& && -> param type is int&
func(std::move(v));   // xvalue:  T = int,  int  && -> param type is int&&
func(v + 1);          // prvalue: T = int,  int  && -> param type is int&&

Reference collapsing is what makes this work: & &, & && and && & all collapse to &, and only && && collapses to &&.

Case C: ParamType is passed by value

C++
template<typename T>
void func(T param);

const int cx = 10;
func(cx);   // T = int

param is a copy, so references and top-level cv-qualifiers are dropped.

Only top-level const is dropped — low-level const is part of the type being copied and survives:

C++
const char* const s = "hi";

func(s);   // T = const char*
           // the top-level const (the pointer) is dropped,
           // the low-level const (what it points to) is kept

2. auto deduction

auto deduction works almost the same as function template argument deduction: auto plays the role of T, and the full declarator plays the role of ParamType.

auto x = expr

Follows the pass-by-value rule (Case C).

auto& x = expr

Follows the lvalue-reference rule (Case A).

auto&& x = expr

Follows the forwarding reference rule (Case B).

C++
int x = 10;
int& r = x;
const int cx = x;

auto a1 = cx;    // a1 is int,        drop const, since pass by value drops top-level const
auto a2 = r;     // a2 is int,        drop reference, since pass by value drops references
auto& r1 = cx;   // r1 is const int&
auto&& a3 = x;   // a3 is int&        (x is an lvalue)
auto&& a4 = 10;  // a4 is int&&       (10 is a prvalue)

The one place auto and template deduction disagree

A braced initializer list. auto has a special rule for it; template deduction has none, so it simply fails to deduce:

C++
auto x = {1, 2, 3};   // ok:    x is std::initializer_list<int>

template<typename T> void func(T param);
func({1, 2, 3});      // error: no matching function for call to
                      //        'func(<brace-enclosed initializer list>)'

This is the difference hiding behind the word "almost".

3. decltype

decltype(variable)

Gives the declared type of the identifier. Top-level cv-qualifiers are preserved, and a reference is present only if the variable was declared as a reference.

C++
int x = 10;
const int cx = x;

decltype(x);    // int
decltype(cx);   // const int

decltype(expr)

Yields the type of the expression, without evaluating it (the operand is unevaluated). Here the result depends on the value category:

  1. If the expression is an lvalue, the result is T&:
C++
int x = 10;
decltype((x));   // int&  — the parentheses make it an expression, not an identifier
  1. If the expression is an xvalue, the result is T&&:
C++
int x = 10;
decltype(std::move(x));   // int&&
  1. If the expression is a prvalue, the result is T:
C++
decltype(10);   // int

decltype(auto)

Infers the type from the initializer, or from the return statement of a function, using the decltype rules rather than the auto rules.

C++
template<typename T>
decltype(auto) pass(T&& arg) {
    return std::forward<T>(arg);
}

decltype(auto) vs auto — decltype(auto) preserves cv-qualifiers and references:

C++
int x = 10;
const int cx = x;

auto c1 = cx;              // int,  drops const

auto a1 = (x);             // int   — auto rules ignore the parentheses
decltype(auto) a2 = (x);   // int&  — decltype rules do not: (x) is an lvalue expression

That last line is the trap, and it is much more dangerous in return position:

C++
decltype(auto) good() {
    int x = 10;
    return x;      // int — fine
}

decltype(auto) bad() {
    int x = 10;
    return (x);    // int& — returns a reference to a local that is already destroyed
}

A single pair of parentheses turns a value return into a dangling reference.