A few days I literally discovered a behaviour of C++, where template arguments are automatically inserted, as shown in this example (nonsensical, only used to show what I mean):
#include <iostream>
template<typename Type> void setVar(Type& subj, const Type& in)
{
subj = static_cast<Type>(in);
}
int main()
{
int foo;
setVar(foo, 42);
std::cout << foo << std::endl;
}
My questions:
- What is this behaviour called?
- are there special rules when and why templates can be automatically inserted?
Template argument deduction.
You cannot say like
templates are inserted. Rather the types of parameters are automatically deduced from the arguments. When and how? That’s what TAD is all about.Check out section
14.8.2in C++03