From the course: C Essential Training

Unlock the full course today

Join today to access over 23,700 courses taught by industry experts.

Template functions

Template functions - C Tutorial

From the course: C Essential Training

Template functions

- [Instructor] C supports both template functions and template classes. In this movie, we'll look at template functions. This is template-function.cpp from Chapter 08 of the Exercise Files. And here we have a simple template function that returns the maximum value of two parameters. You notice the template keyword, and in these angle brackets here, we have the template argument list. The keyword typename is commonly used here, but the word class works exactly the same and is often used instead. Just be aware, that at least in this context, either of these two tokens means the same thing. I strongly recommend that you use typename. You may see class used in legacy code, but typename is less ambiguous. So this simple template function is both type agnostic and type safe. It's type agnostic because it'll work for any class or type that supports the greater than operator for comparisons. It's type safe because the…

Contents