cpp/language/class template argument deduction

In order to instantiate a, every template argument must be known, but not every template argument has to be specified. In the following contexts the compiler will deduce the template arguments from the type of the initializer:
 * any that specifies initialization of a variable and variable template, whose declared type is the class template (possibly ):




 * expressions:

Implicitly-generated deduction guides
When, in a function-style cast or in a variable's declaration, the type specifier consists solely of the name of a primary class template (i.e., there is no accompanying template argument list), candidates for deduction are formed as follows:


 * If is defined, for each constructor (or constructor template)  declared in the named primary template, a fictional function template, is constructed, such that
 * template parameters of are the template parameters of  followed (if  is a constructor template) by the template parameters of  (default template arguments are included too)
 * the function parameters of are the constructor parameters
 * the return type of is  followed by the template parameters of the class template enclosed in


 * If is not defined or does not declare any constructors, an additional fictional function template is added, derived as above from a hypothetical constructor
 * In any case, an additional fictional function template derived as above from a hypothetical constructor is added, called the copy deduction candidate.

and is then performed for initialization of a fictional object of hypothetical class type, whose constructor signatures match the guides (except for return type) for the purpose of forming an overload set, and the initializer is provided by the context in which class template argument deduction was performed, except that  the first phase of  (considering initializer-list constructors) is omitted if the initializer list consists of a single expression of type (possibly cv-qualified), where  is a specialization of  or a class derived from a specialization of.

These fictional constructors are public members of the hypothetical class type. They are explicit if the guide was formed from an explicit constructor. If overload resolution fails, the program is ill-formed. Otherwise, the return type of the selected template specialization becomes the deduced class template specialization.

Or, for a more complex example (note: "" would not compile: scope resolution qualifiers are not something that can be deduced):

User-defined deduction guides
The syntax of a user-defined deduction guide is the syntax of a function declaration with a trailing return type, except that it uses the name of a class template as the function name:

User-defined deduction guides must name a class template and must be introduced within the same semantic scope of the class template (which could be namespace or enclosing class) and, for a member class template, must have the same access, but deduction guides do not become members of that scope.

A deduction guide is not a function and does not have a body. Deduction guides are not found by name lookup and do not participate in overload resolution except for the when deducing class template arguments. Deduction guides cannot be redeclared in the same translation unit for the same class template.

The fictional constructors for the purpose of overload resolution (described above) are explicit if they correspond to an implicitly-generated deduction guide formed from an explicit constructor or to a user-defined deduction guide that is declared. As always, such constructors are ignored in copy-initialization context:

Using a member typedef or alias template in a constructor or constructor template's parameter list does not, by itself, render the corresponding parameter of the implicitly generated guide a non-deduced context.

{{rrev|since=c++20|1=

Deduction for alias templates
When a function-style cast or declaration of a variable uses the name of an alias template without an argument list as the type specifier, where  is defined as an alias of, the scope of  is non-dependent, and  is either a class template or a similarly-defined alias template, deduction will proceed in the same way as for class templates, except that the guides are instead generated from the guides of , as follows:


 * For each guide of, deduce the template arguments of the return type of  from  using , except that deduction does not fail if some arguments are not deduced. If deduction fails for another reason, proceed with an empty set of deduced template arguments.
 * Substitute the result of above deduction into, if substitution fails, no guide is produced; otherwise, let denote the result of substitution, a guide  is formed, such that
 * The parameter types and the return type of are the same as
 * If is a template,  is a function template whose template parameter list consists of all the template parameters of  (including their default template arguments) that appear in the above deductions or (recursively) in their default template arguments, followed by the template parameters of  that were not deduced (including their default template arguments); otherwise ( is not a template),  is a function
 * The associated of  are the conjunction of the associated constraints of  and a constraint that is satisfied if and only if the arguments of  are deducible from the result type

}}