You missed that there are implicitly-generated deduction guides that work for the majority of cases.
Anyway the point of CTAD is to figure out the type of a class from the type of its constructor arguments.
Here's an example of a class template that benefits from a user-defined deduction guide:
typename <typename T, int N> struct my_array { T data[N]; };
We'd like the compiler to deduce that the type of
my_array{ 1, 2, 3 } is
my_array<int, 3>, but it can't do that without help.
We can define a deduction guide to help the compiler.
1 2
|
template <typename T, typename... Ts>
my_array(T, Ts...) -> my_array<T, 1 + sizeof... (Ts)>;
|
Note the deduction guide looks like a declaration of a constructor template with a trailing return type.
The compiler makes one
fake function template for every deduction guide.
For example the compiler invents this function from the deduction guide above:
1 2
|
template <typename T, typename... Ts>
auto my_array(T, Ts...) -> my_array<T, 1 + sizeof... (Ts)>;
|
Then, when the compiler encounters
my_array{ 1, 2, 3 }
It "calls" the fake function template (or templates, if there are more deduction guides) with the constructor arguments:
my_array(1, 2, 3) // returns my_array<int, 3>
And uses the type of that expression as the type of the class.
Please note this is simplified.