Function Templates and Class Templates
White PapersMore >>
- Advanced Threat Protection For Dummies ebook and Using Big Data Security Analytics to Identify Advanced Threats Webcast
- How to Create an End-to-End Enterprise Payments Hub
In addition to being able to create function libraries and class libraries with C++, you can create libraries containing function templates and class templates, leaving the task of instantiating such templates to the application program.
Often, the actual template instantiation parameters are not foreseeable by the library developers. They can only specify in the documentation that those parameters must satisfy certain requisites. If the templates are instantiated with parameters that do not respect those requisites, the compiler emits error messages.
For example, the Standard Library contains the function templates
max, and the class templates
The function template
min can be instantiated with a numeric type or with the
string type, but not with the
complex<T> type, for any
The class template
vector can be instantiated with any type if the only goal is to create an empty collection. However, if you want to call the
resize member function of that collection, the
element type must have a default public constructor or no constructor at all.
Libraries as Abstractions: Avoiding Errors
When designing a library, you should consider not only the features you want to provide for application programmers, but also the programming errors that you want to prevent.
Actually, sometimes the purpose of a specific feature of a library is not so much to provide a functionality to users as it is to prevent programming errors by forbidding error-prone operations. For example, a time instant and a time span are different kinds of entities. You can add two time spans and you can multiply a time span by a number.
However, you cannot add two time instants nor multiply a time instant by a number. You can subtract a time instant from another time instant, obtaining the time span between them, and you can sum a time span to a time instant, obtaining another time instant, following the former by that time span.
If a library defines a class for time instants and another class for time spans, it should allow only the operations that make sense for every class. The application code that would contain disallowed operations shouldn't be accepted by a compiler.
The most advanced and sophisticated C++ libraries define recursive templates in a programming style called "template metaprogramming." In this context, a logical programming error (almost) always generates a compilation error. In general, the wrong usage of forbidden expressions should, if possible, result in compile-time errors. To this purpose, some libraries (Boost and Loki, for instance) have defined macros to declare compile-time assertions.
If a program using the Loki library contains the statement
STATIC_CHECK(3 == 4) when it is compiled, an error is generated. The same happens if a program using the Boost libraries and containing the statement
BOOST_STATIC_ASSERT(3 == 4) is compiled.
Such statements, similar to the
assert macro from the C Standard Library, allow the compiler to analyze a constant expression, and if the expression comes out as false, they generate a compile-time error.