Skip to content

Class template argument deduction (CTAD) (since C++17)

cppreference Class template argument deduction (CTAD) (since C++17)

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:

Deduction for class templates

Implicitly-generated deduction guides

User-defined deduction guides

Deduction for alias templates(since C++20)

CTAD is replacement for object generator pattern

在阅读stackoverflow Current status of std::make_array # A

C++17 includes template argument deduction for classes, so you can now write:

std::pair p (foo, bar);
std::array arr = { 1, 2, 3, 4, 5 };

and so on. But there are some (somewhat subtle) remaining use cases where make_pair or make_array can be useful, and you can read about them in: Usefulness of std::make_pair and std::make_tuple in C++1z

显然 C++17 Class template argument deduction (CTAD) 在一定程度上可以取代 object generator pattern。