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 - C++ Tutorial
From the course: Complete Guide to C++ Programming Foundations
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
-
-
-
-
-
-
-
-
-
-
(Locked)
Understanding templates3m 1s
-
(Locked)
Template functions3m 5s
-
(Locked)
Template classes2m 39s
-
(Locked)
Overview of the STL2m 16s
-
(Locked)
Queues and stacks4m 19s
-
(Locked)
Advanced concepts: Iterators6m 10s
-
(Locked)
Advanced concepts: Algorithms3m 27s
-
(Locked)
Solution: Virtual pet schedule1m 48s
-
(Locked)
-
-