Function templates allow writing a single definition that can handle multiple different types. It is a very powerful form of C++’s static polymorphism.
When instantiating a class template, we have to pass in the types explictly (at least until C++17):
std::vector<int> vec; std::basic_string<my_char, std::char_traits<my_char>> str; std::tuple<int, bool, std::string> tuple;
But when instantiating a function template, the compiler can often figure the types out:
template <typename A, typename B, typename C> void func(const A& a, const B& b, const C& c); … int x; func(x, 'A', "hello"); // equivalent to: func<int, char, const char*>(x, 'A', "hello");
Let’s look at this process into a little bit more detail and establish some guidelines as well as see how we can prohibit template argument deduction for arguments.» read more »