From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Template functions

Template functions

- [Instructor] In the last video, we introduced templates and saw this code snippet that defines a template function. Now, it's time to run it and see how it works. Let's begin by looking at the code. In line 10, we have the template function called size in bits. This function uses the template keyword followed by type name T in angle brackets, indicating that T is a placeholder for any data type. The function takes one argument of type T, calculates its size in bytes, using the size of operator, and then multiplies by eight to get the size in bits. Starting at line 15 in the main function, we already have some function calls to print the size in bits of an integer, a character, a float, and a double. Let's run it to see what happens. As you can see, it returns a sizing bits of 32 for the integer, eight for the character, 32 for the float, and 64 for the double. Now, let's explore some more interesting types. So, let me add a separator at this point. So in line 21, let's use a short…

Contents