C++ Template Meta Programming

This is a short series of articles about C++ template meta programming. Template metaprogramming is a programming technique in which templates are used by the C++ compiler to generate temporary source code. The temporary code is merged by the compiler with the rest of the source code and then compiled.

The articles start with an overview of type lists, moving on to MicroStation element type lists. We proceed to show how type lists can enable static polymorphic class inheritance. This provides a tool to add behavioural traits to a class as it inherits from multiple simple classes. Next, we use that polymorphic technique to create a framework of classes for handling MicroStation elements. We show how you can use either dynamic (i.e. a set of pure virtual functions) or static (using the curious recurring template pattern) polymorphism to design a set of classes.


The C++ language continues to evolve. Two evolutionary steps were the introduction of templates and Alexander Stepanov's invention of the Standard Template Library (STL). The STL is incorporated into the C++ language standard.

An unexpected consequence of the introduction of templates was template metaprogramming. Template metaprogramming provides a mechanism to write a compile-time program. In other words, abstract application logic in a way that lets the C++ compiler check its correctness.

Template metaprogramming leads to new areas of program capability. In this article, we'll examine type lists in the context of MicroStation MDL element types. We'll be using concepts and idioms from the C++ Standard Library, the Boost Libraries and in particular the Boost Metaprogramming Library (MPL).

C++ 11

C++ 11 has changed the playing field. The addition of variadic templates with their associated parameter packs added a compile-time list of types structure directly into the language. With C++ 11, type lists are as easy as …

// C++11
template<class... T> struct type_list {};

C++ Standard Library

Some examples assume that you are familiar with the C++ Standard Library. That is, classes such as std::string and std::vector should already be your friends.

Boost libraries


The Element Type Lists discussed in this series use the Boost libraries, particularly the Meta Programming Library (boost::mpl).

The Boost libraries are not part of the C++ standard. They are developed independently of C++ and inform its development. For example, the latest C++ standard includes several libraries that originated at Boost.

If you want to use the Boost libraries, you must …

  1. Download the appropriate library. See www.boost.org for details. Pre-built binary installations are available for Visual Studio 2005 (which is the version of Visual Studio you need to develop applications for MicroStation V8i). Boost libraries are compatible with many other C++ compilers
  2. Install the Boost libraries on your development computer. Installation involves no executables — simply copy the header and library files to the \boost directory

C++ Template Metaprogramming Books

If you're new to template metaprogramming, and it makes you feel the way you did when you first saw a de-referenced pointer in C, then buy the C++ Template Metaprogramming book by David Abrahams and Aleksey Gurtovoy. It helps you to cut an enlightened path through the Boost documentation, which can seem turbid at times1 .

C++ Template Metaprogramming

Another invaluable reference, written by Andrie Alexandrescu, is Modern C++ Design.

Modern C++ Design

Type Lists and Template Meta Programming

The following articles discuss C++ type lists, and some uses for them. Type lists are enabled by template C++ metaprogramming techniques. By template metaprogramming techniques, we mean the Boost Meta Programming Language, or MPL. We move on to show how type lists can enable selective inheritance. Selective inheritance means static polymorphism, which enables the compiler to check the design of our polymorphic classes. Next, we show a polymorphic element class for MicroStation developers, which is informed by the content of the preceding articles. We demonstrate two sets of classes that implement dynamic or static polymorphism. Finally, we demonstrate a simple factory implementation, informed by Boost.

Index (the page you are reading): Metaprogramming and the MPL introduction
MPL: type lists Type Lists and the MPL
Object Inheritance Conditional Inheritance and the MPL
Polymorphic Element Classes Polymorphic Classes for MicroStation Elements
ElementPolymorphicDynamic.h Dynamic Polymorphic Element header file overview
ElementPolymorphicStatic.h Static Polymorphic Element header file overview
Element Factory Element Factory

MicroStation and Application Development

MicroStation® is a computer-aided-design (CAD) tool produced by Bentley Systems. It is used by engineers, architects, and draughting technicians to create 3D models and 2D drawings.

MicroStation can be customised. Bentley Systems themselves have many applications for specialised markets that use MicroStation as a platform. Third-parties likewise may use MicroStation as a platform on which to develop their application software. Bentley Systems provide the MicroStation Software Development Kit (SDK) that software developers use to create their applications.

The SDK has two libraries: the MicroStation Development Library (MDL) and the MicroStationAPI. The MicroStation Development Library contains thousands of functions that provide a C-language interface. The MicroStationAPI provides a rich C++ interface.

Development Tool Versions

We use development tools compatible with MicroStation V8i, which are the C++ compiler and linker provided with Visual Studio 2005. The example code in these articles was built and tested with …

If using a later version of MicroStation, you'll need to check the version of Visual Studio that provides compatible tools. Check this table of Visual Studio versions to match MicroStation and Visual Studio versions.

Element Types Header File

LA Solutions created the ElementTypes.h header file. It creates a unique C++ type for each element type defined in the MDL enumeration enum MSElementTypes in file mselems.h. If you're a MicroStation developer, you already have that header file (mselems.h) delivered with the MicroStation SDK. You'll find it in the \MicroStation\mdl\include folder.

You can download ElementTypes.h. We've included another header file that you may find useful (UndefineMdlMacrosThatInterfereWithBoost.h).

Footnote 1: Boost Documentation

Some might comment that the Boost documentation is turbid all the time, but that's unfair. Keep in mind that the authors of Boost are …

  1. outstanding programmers
  2. working pro bono
  3. annotating their own work

The book helps, in the case of the MPL, interpret those annotations.