Writing C++ metafunctions procedurally
Author:Jyrki Katajainen
Publication:CPH STL report 2020-2, Department of Computer Science, University of Copenhagen (2020), 22 pp.
Full text:<pdf.gif>PDF (312 kB)  
Abstract:In this paper, we describe the design and implementation of the compile-time functions (metafunctions) available in the CPH MPL (Copenhagen metaprogramming library) and the CPH STL (Copenhagen standard template library). Our main point is to show that, after the addition of constant-expression functions to C++, the users need to write very few template metaprograms. This is a good thing when it comes to program readability and maintainability. When explaining the design, we dissect the implementation of four compile-time functions:
Numeric function:
lg: integer → unsigned integer
Compute the whole-number binary logarithm of the absolute value |x| of an integer x, i.e. ⌊log2 |x|⌋.
Type attribute:
width: type → unsigned integer
Return the number of bits in the value representation of an object of the specified type.
Type association:
difference: type → associated type
Provide an alias for the difference type to be used for recording the distance between two values specified by the given type.
Type selection:
first_wide_enough: typelist, integer → type
Return the first type in the given list of types whose width is larger than or equal to the given integer.

As a small side result we show that, for an integer x of type cphstl::integer<>, the whole-number binary logarithm of its absolute value |x|, i.e. ⌊log2 |x|⌋, can be computed with a constant number of word operations, even if the size of the number is unlimited.

Related:<html.gif>HTML (Source code)  
  author = {Jyrki Katajainen},
  title = {Writing C\texttt{++} metafunctions procedurally},
  type = {CPH STL report},
  number = {2020-2},
  institution = {Department of Computer Science, University of Copenhagen},
  year = {2020},
  pagetotal = {22},
This page was generated by Jyrki Katajainen <jyrki@di.ku.dk> on 2020-05-12.