From the course: C++ Standard Template Library

Generic programming - C++ Tutorial

From the course: C++ Standard Template Library

Start my 1-month free trial

Generic programming

- [Narrator] To give you a clear view as to why the C++ Standard Template Library is special, we'll now talk about Generic Programming. Generic Programming is a programming paradigm in which data types are not specified when functions or other reusable pieces of code are written. Instead, data types are specified in the lines of code that instantiate, or otherwise use those pieces of reusable code. Here are two important papers that summarize the evolution of Generic Programming, and its culmination in the C++ Standard Template Library. Computer Scientist, Alexander Stepanov, along with a few of his colleagues since the late-1970s has been a well-known advocate of Generic Programming. He first coined the term in 1989 on a paper titled "Generic Programming." Sometime later he worked on the early stages of what we now know as the C++ STL, until its initial release by Hewlett-Packard in 1994. Both papers are available at stepanovpapers.com. Generic Programming is sometimes referred to as Compile-Time Polymorphism because specifying data types at variable definition for object instantiation is, in essence, polymorphism resolved at compile time. This type of polymorphism may be computationally less expensive than the traditional runtime polymorphism you may be familiar with in object-oriented programming. That's because most of the calculations and adaptations that need to be performed to achieve polymorphism will happen once when the application is being built by the compiler, instead of using valuable CPU time for this. The resulting code will naturally have the intended behavior. Now, C++ enables Generic Programming by means of special constructs called Templates. Most programming languages use other names for generic types, but I personally like the term template because that's what it is, a template. Just like a template you may use to create a slideshow presentation, or a word processor document. Well, a C++ template is the same thing, but for pieces of code. Let's see a brief example to illustrate the syntax of a template in C++. The first two lines here, will allow us to use the cout object to print text on the terminal. So first, a template is defined with the keyword lowercase template, followed by the definition of a symbol that will serve as the parameter in the template. That's enclosed in angle brackets. Templates are commonly used to define functions and classes. Here we have a typing definition of the symbol capital T. Then we'll define a function, size in bits that will return an integer. It receives one parameter, A of type T, whatever this type may be. The function will return the size of the type of A in bytes, times eight. That is to say, its size in bits. Now let's see how we can use that function with four different data types. Here's a main function that prints out the results of this function, according to the data type it receives. First, we will send in an integer constant. Then a character constant. Next, a float constant. And finally, a double constant. Keep in mind that depending on your system, the data type sizes may vary. So let's compile and run. As you can see, I'm getting 32 bits for int, 8 bits for char, 32 bits for float, and 64 bits for double. So there you have it, our function size in bits was defined generically so that we can use it for different data types later.

Contents