Function template argument deduction
Earlier in this book, we have briefly talked about the fact that the compiler can sometimes deduce the template arguments from the context of the function call, allowing you to avoid explicitly specifying them. The rules for template argument deduction are more complex and we will explore this topic in this section.
Let’s start the discussion by looking at a simple example:
template <typename T> void process(T arg) { std::cout << "process " << arg << '\n'; } int main() { process(42); // [1] T is int process<int>(42); // [2] T is int, redundant process<short>(42); // [3] T is short }
In this snippet, process
is a function template with a single type template parameter. The calls process(42)
and process...