Advertisement
Advanced C++, Type Deduction & auto

What is class template argument deduction (CTAD)?

Short answer

CTAD (C++17) lets you construct a class template without specifying its template arguments—the compiler deduces them from the constructor arguments, similar to function template deduction. It reduces verbosity for types like std::pair, std::vector, and std::lock_guard.

Save C++ mock interview
Advertisement
AModel answer
CTAD (C++17) lets you construct a class template without specifying its template arguments—the compiler deduces them from the constructor arguments, similar to function template deduction. It reduces verbosity for types like std::pair, std::vector, and std::lock_guard. Deduction guides can customize how arguments map to template parameters.
std::pair p(1, 2.0);        // pair<int, double> deduced
std::vector v{1, 2, 3};     // vector<int>
std::lock_guard g(mtx);     // lock_guard<mutex>
Real-world example
CTAD lets you write std::lock_guard g(mtx) without repeating the mutex type.
Likely follow-ups
  • What are deduction guides? | Which standard types benefit from CTAD?
Related topics