Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Class template argument deduction (CTAD)

Reference for C++17 class template argument deduction: implicit and user-defined deduction guides, aggregates, std::vector, std::pair and common pitfalls.

Class template argument deduction (CTAD) lets the compiler work out the template arguments of a class from its constructor arguments, so you can write std::pair p{1, 2.5} instead of std::pair<int, double> p{1, 2.5}. It was added in C++17. Function templates have always deduced their arguments; CTAD brings the same convenience to class templates.

The compiler builds a set of hidden function templates, called deduction candidates, from the class's constructors, runs ordinary overload resolution on them, and uses the winner's deduced parameters as the class arguments. You can add your own deduction guides to steer the result. Since C++20 aggregates and alias templates take part as well.

C++
std::vector v{1, 2, 3};        // std::vector<int>
std::pair p{"id", 42};         // std::pair<const char*, int>

Try it

C++ Deduction from constructors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ User-defined deduction guides

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

An implicit candidate is generated for every constructor, plus one for the copy deduction candidate. A user-defined deduction guide looks like a function declaration with a trailing return type that names the template:

C++
template <template-parameters>
explicit(optional) ClassName(parameter-list) -> ClassName<template-arguments>;
Part Description
ClassName(parameter-list) The constructor-call shape the guide matches. Parameters are used for deduction only.
-> ClassName<args> The specialization chosen when the guide wins overload resolution.
explicit Makes the guide ignored for copy-list-initialization (= {...}).

Errors: if no candidate is viable, or two are equally good, the program is ill-formed with a "class template argument deduction failed" diagnostic. There are no runtime effects.

Usage notes

  • Where it applies: variable declarations (std::vector v{...}), new expressions, function-style casts (std::pair(1, 2)), and, since C++20, template arguments of non-type parameters of class type. It does not apply to function parameters or member declarations.
  • No partial deduction: you either write all template arguments or none. std::pair<int> p{1, 2} is an error. Default template arguments are used when they exist.
  • Copy deduction beats wrapping: std::vector v2{v1} with v1 a vector<int> makes a copy (vector<int>), not a vector<vector<int>>. Use std::vector<std::vector<int>> explicitly to nest. This is an easy source of surprise with braces.
  • Braces prefer initializer_list: std::vector v{1, 2} is a vector<int> holding two elements, not a vector of size 1 filled with 2. Parentheses call the other constructors.
  • String literals deduce const char*: std::vector v{"a", "b"} is vector<const char*>, not strings. Write std::string("a") or use the s literal if you want std::string.
  • Aggregates (C++20): simple structs with no constructors deduce from their initializers. In Named, T appears only in the second member, so the guide above is what lets {"count", 3} deduce Named<int> while the first member converts to std::string.
  • Alias templates (C++20): deduction works through template <class T> using Vec = std::vector<T>; as well.
  • Standard library guides: std::array, std::pair, std::tuple, std::optional, std::lock_guard, std::function (from a function pointer or callable) and the containers all ship with guides. A common use is std::lock_guard lock(mutex);.
  • Pitfall: CTAD is convenient but can hide a type. If a deduced type surprises you, print typeid or spell the arguments out.