C++ Succinctly: Templates

Introduction

Template functions and classes serve a similar purpose in C++ as generics serve in C#. They allow you to reuse your code without writing a function or class for each variant you want. As long as the types you supply to the template have the functionality associated with them that the template uses, all is well. If not, then the compiler will generate an error. This is because the compiler is generating a unique class for each specialization you use. Because the compiler builds classes and functions from your program’s templates, template functions and classes must be put into header files and defined entirely inline. That way, the compiler can parse them for all source code files that use them.

Ultimately, templates can become very complex. The C++ Standard Library demonstrates the power and complexity of advanced templates. Despite that, you do not need an advanced knowledge of templates to use them effectively. An understanding of the fundamentals of C++ templates will help you unlock a significant amount of functionality and power.


Template Functions

A template function is a stand-alone function that takes at least one template argument. The fact that it takes an argument makes it incomplete until it is called with a concrete argument, thereby causing the template to become a fully defined function. Here is a template function that takes in two arguments.

Sample: TemplatesSample\PeekLastItem.h

The creation of any template—function or class—starts with the keyword template followed by the parameters within arrow brackets, as seen in the previous sample with class T and class U. The use of the word class before T and U does not mean those arguments must be classes. Think of class instead as a general word intended to convey the meaning of a non-specific type. You could have a template with concrete types or with a mixture of non-specific class types and concrete types, such as template<class T, int>. The use of T as the name for the first argument and U for a second is a common practice, not a requirement. You could use almost anything as a template argument name.

The previous function takes in a reference to an item of type T. It presumes that T will have a member function called rbegin, which can be called with no arguments and will return a pointer type that, when de-referenced, will become an object of type U. This particular function is designed primarily to work with many of the C++ Standard Library’s collection classes, though any class that meets the assumptions the function makes about type T can be used with this template function. That ability to take any type, add the requisite functionality to it, and thereby make it eligible for use with a template is the main draw of templates.


Template Classes

Template classes are similar to template functions, only they are classes rather than simple stand-alone functions. Let’s look at an example.

Sample: TemplatesSample\SimpleMath.h

As the name implies, this class is not meant to be more than a demonstration. There is only one type argument. Looking at the class definition, we can deduce that the requirements for T in this case are that it has the following operators, all of which operate on two instances of T and return an instance of T:

  • +
  • -
  • *
  • /

While these logically belong with numbers, you can define these operators for any class or data type and then instantiate an instance of this template class, which is specialized for your custom class (e.g., a Matrix class).

One last note, if you were to define the member functions outside of the class definition, but still inside the same header file, of course, then you would need to use the inline keyword in the declarations; the definitions would look like this: SimpleMath::SimpleMath(void) { }.

This is the last file of this sample, showing a simple usage of each of the preceding templates.

Sample: TemplatesSample\TemplatesSample.cpp

Conclusion

Template functions help you reuse code, which in turn makes your code base easier to manage and DRY (Don't Repeat Yourself). Lambda expressions are the subject of the next article of this series.

This lesson represents a chapter from C++ Succinctly, a free eBook from the team at Syncfusion.
Tags:

Comments

Related Articles