Class template argument deduction (CTAD) (since C++17)
From cppreference.com
In order to instantiate a class template, 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 declaration that specifies initialization of a variable and variable template, whose declared type is the class template (possibly cv-qualified):
std::pair p(2, 4.5); // deduces to std::pair<int, double> p(2, 4.5); std::tuple t(4, 3, 2.5); // same as auto t = std::make_tuple(4, 3, 2.5); std::less l; // same as std::less<void> l;
template<class T> struct A { A(T, T); }; auto y = new A{1, 2}; // allocated type is A<int>
- function-style cast expressions:
auto lck = std::lock_guard(mtx); // deduces to std::lock_guard<std::mutex> std::copy_n(vi1, 3, std::back_insert_iterator(vi2)); // deduces to std::back_insert_iterator<T>, // where T is the type of the container vi2 std::for_each(vi.begin(), vi.end(), Foo([&](int i) {...})); // deduces to Foo<T>, // where T is the unique lambda type
template<class T> struct X { constexpr X(T) {} }; template<X x> struct Y {}; Y<0> y; // OK, Y<X<int>(0)> |
(since C++20) |
Contents |
[edit] Deduction for class templates
[edit] 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 C
(i.e., there is no accompanying template argument list), candidates for deduction are formed as follows:
- If
C
is defined, for each constructor (or constructor template)Ci
declared in the named primary template, a fictional function templateFi
, is constructed, such that all following conditions are satisfied:
- The template parameters of
Fi
are the template parameters ofC
followed (ifCi
is a constructor template) by the template parameters ofCi
(default template arguments are included too).
- The template parameters of
|
(since C++20) |
- The parameter list of
Fi
is the parameter list ofCi
. - The return type of
Fi
isC
followed by the template parameters of the class template enclosed in<>
.
- The parameter list of
- If
C
is not defined or does not declare any constructors, an additional fictional function template is added, derived as above from a hypothetical constructorC()
.
- In any case, an additional fictional function template derived as above from a hypothetical constructor
C(C)
is added, called the copy deduction candidate.
- For each user-defined deduction guide
Gi
, a fictional function or function templateFi
, is constructed, such that all following conditions are satisfied:
- The parameter list of
Fi
is the parameter list ofGi
. - The return type of
Fi
is the simple template identifier ofGi
. - If
Gi
has template parameters (syntax (2)),Fi
is a function template, and its template parameter list is the template parameter list ofGi
. Otherwise,Fi
is a function.
- The parameter list of
template<class T> struct A { T t; struct { long a, b; } u; }; A a{1, 2, 3}; // aggregate deduction candidate: // template<class T> // A<T> F(T, long, long); template<class... Args> struct B : std::tuple<Args...>, Args... {}; B b{std::tuple<std::any, std::string>{}, |