© Copyright Vesa Karvonen 2001. All rights reserved.


TMPL - a C++ template metaprogramming language

This document describes TMPL, a C++ template metaprogramming language, which is a subset of the C++ programming language.

TMPL is basically a:

programming language.

General

The TMPL language is a subset of the C++ programming language. The semantics of TMPL are a subset of the semantics of C++. Therefore the semantics of TMPL are not described in this document.

This document can be viewed as a set of guidelines for structured, instead of ad-hoc, template metaprogramming. Departing from these guidelines, such as using top-level templates, generally leads to less flexible template metacode (although may locally result in smaller amount of C++ code).

In order to avoid confusion with C++ concepts, TMPL language concepts, that are traditionally called types, values, functions, etc... are called metatypes, metavalues, metafunctions, etc..., respectively. It should be noted, that TMPL metatypes, for instance, can not, in general, be described by a single C++ type or any other singular C++ construct.

Introduction to TMPL concepts

Metavalues

A metavalue is a member of a metatype.

C++ types as metavalues

A metavalue is a C++ type.

[Note that integer constants are not metavalues in TMPL, but a metavalue may denote an integral value. For example, the built-in metatype:

template
< int value_param
>
struct integer
{ enum
  { value = value_param
  };
};

denotes integral values. The value of an integer<> computed by TMPL code can naturally be accessed in C++.]

Metatypes

A metatype is set of metavalues.

Built-in metatypes: boolean<bool> and integer<int>

TMPL has two special built-in metatypes, which are C++ class templates. These metatypes are:

boolean<bool>
A boolean type. Literals take the form boolean<value>, where value is a C++ boolean constant expression.
integer<int>
A signed integer type. Literals take the form: integer<value>, where value is a C++ integral constant expression.

Both of these built-in metatypes contain a public static integral constant named value.

Metafunctions

Metatypes whose metavalues can be expanded as follows are metafunctions:

typename metafunction::template code<[p0,p1,...,pn]>::type

[The purpose of this form is to allow maximum flexibility in combining and manipulating metafunctions.]

[Note that template is required when the metafunction is a dependent type and the typename is required when template is required or when a parameter is a dependent type.]

[Intuitively, metafunctions are C++ types, that have the following structure:

struct metafunction
{ template<class p0[, class p1, ..., class pn]>
  struct code
  { typedef ... type;
  };
};]

[Note that integral constants are never used as metafunction parameters or returned by a metafunction.]

Suspensions

Metavalues that can be expanded as follows are suspensions:

typename suspension::type

[Note that typename is required when the suspension is a dependent type.]

[Suspensions are useful for introducing laziness into the otherwise too strict C++ template mechanism.]

Algebraic metatypes or Modules

Metatypes whose metavalues consist of named inner:

  • metaconstructors, and
  • metafunctions.

are called algebraic metatypes or modules.

A metaconstructor is either a metavalue or a metafunction.

[Note that in TMPL a single mechamism combines the features of both algeraic types and modules found in modern functional programming languages such as Haskell.]

[Note that an algebraic metatype or module may be recursive. This means that a metaconstructor may accept as parameter the result of a metaconstructor of the same algebraic metatype or module.]

[Intuitively, the C++ type of an algebraic metatype or module has the following structure:

struct algebraic_metatype_or_module
{ // --- metaconstructors ---
  struct metavalue_like_constructor
  { ...
  };

  struct metafunction_like_constructor
  { template<class p0[, class p1, ..., class pn]>
    struct code
    { typedef ... type;
    };
  };

  // ...

  // --- metafunctions ---
  struct metafunction
  { template<class p0[, class p1, ..., class pn]>
    struct code
    { typedef ... type;
    };
  };

  // ...
};]

© Copyright Vesa Karvonen 2001. All rights reserved.