Channels ▼
RSS

Testing

Static Testing C++ Code


Function Templates and Class Templates

More Insights

White Papers

More >>

Reports

More >>

Webcasts

More >>

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 min and max, and the class templates vector and list.

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 T.

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.


Related Reading






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video