January 2019
Intermediate to advanced
512 pages
14h 5m
English
Type deduction and substitution are closely related, but not exactly the same. Deduction is the process of guessing—What should the template type, or types, be in order to match the call? Of course, the compiler does not really guess, but applies a set of rules defined in the standard. Consider the following example:
template <typename T> void f(T i, T* p) { std::cout << "f(T, T8)" << std::endl;}int i;f(5, &i); // T == intf(5l, &i); // ?
When considering the first call, we can deduce from the first argument that the T template parameter should be int. Thus, int is substituted for T in both parameters of the function. The template is instantiated as f(int, int*) and is an exact match for the argument types. ...