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.
std::vector v{1, 2, 3}; // std::vector<int> std::pair p{"id", 42}; // std::pair<const char*, int>
Try it
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.
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:
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{...}),newexpressions, 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}withv1avector<int>makes a copy (vector<int>), not avector<vector<int>>. Usestd::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 avector<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"}isvector<const char*>, not strings. Writestd::string("a")or use thesliteral if you wantstd::string. - Aggregates (C++20): simple structs with no constructors deduce from their initializers. In
Named,Tappears only in the second member, so the guide above is what lets{"count", 3}deduceNamed<int>while the first member converts tostd::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 isstd::lock_guard lock(mutex);. - Pitfall: CTAD is convenient but can hide a type. If a deduced type surprises you, print
typeidor spell the arguments out.