From the course: C++ Templates and the STL

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Argument deduction

Argument deduction

- [Instructor] When you call a template function, the compiler must know the types of any arguments, but that doesn't mean you always need to specify all the types. If the compiler can easily deduce the types, then you don't need to specify them at all, this is called template argument deduction, and here I have a working copy of template-function.cpp from chapter one of the exercise files, and you notice down here on line 16, I'm calling our max of template function, and I provided the type of int in the template arguments. So when I build and run, you notice that it knows that this is of type int, and everything works fine. What's interesting though is that it's easy for the compiler to deduce this type from the context, because I'm passing it two ints. So if I remove that template argument and the angle brackets, and I save, and build, and run, you notice that it still works just fine, and this is because the compiler is able to deduce this type from the context. So here's another…

Contents