Home > Articles > Programming > C/C++

An In-Depth Look at Metafunctions in C++

This chapter looks at a practical example from science and engineering that can find applications in almost any numerical code. Along the way, you'll learn some important new concepts and get a taste of metaprogramming at a high level using the MPL.
This chapter is from the book

With the foundation laid so far, we're ready to explore one of the most basic uses for template metaprogramming techniques: adding static type checking to traditionally unchecked operations. We'll look at a practical example from science and engineering that can find applications in almost any numerical code. Along the way you'll learn some important new concepts and get a taste of metaprogramming at a high level using the MPL.

3.1 Dimensional Analysis

The first rule of doing physical calculations on paper is that the numbers being manipulated don't stand alone: most quantities have attached dimensions, to be ignored at our peril. As computations become more complex, keeping track of dimensions is what keeps us from inadvertently assigning a mass to what should be a length or adding acceleration to velocity—it establishes a type system for numbers.

Manual checking of types is tedious, and as a result, it's also error-prone. When human beings become bored, their attention wanders and they tend to make mistakes. Doesn't type checking seem like the sort of job a computer might be good at, though? If we could establish a framework of C++ types for dimensions and quantities, we might be able to catch errors in formulae before they cause serious problems in the real world.

Preventing quantities with different dimensions from interoperating isn't hard; we could simply represent dimensions as classes that only work with dimensions of the same type. What makes this problem interesting is that different dimensions can be combined, via multiplication or division, to produce arbitrarily complex new dimensions. For example, take Newton's law, which relates force to mass and acceleration:

037equ01.jpg

Since mass and acceleration have different dimensions, the dimensions of force must somehow capture their combination. In fact, the dimensions of acceleration are already just such a composite, a change in velocity over time:

037equ02.jpg

Since velocity is just change in distance (l) over time (t), the fundamental dimensions of acceleration are:

038equ01.jpg

And indeed, acceleration is commonly measured in "meters per second squared." It follows that the dimensions of force must be:

038equ02.jpg

and force is commonly measured in kg(m/s2), or "kilogram-meters per second squared." When multiplying quantities of mass and acceleration, we multiply their dimensions as well and carry the result along, which helps us to ensure that the result is meaningful. The formal name for this bookkeeping is dimensional analysis, and our next task will be to implement its rules in the C++ type system. John Barton and Lee Nackman were the first to show how to do this in their seminal book, Scientific and Engineering C++ [BN94]. We will recast their approach here in metaprogramming terms.

3.1.1 Representing Dimensions

An international standard called Système International d'Unites breaks every quantity down into a combination of the dimensions mass, length or position, time, charge, temperature, intensity, and angle. To be reasonably general, our system would have to be able to represent seven or more fundamental dimensions. It also needs the ability to represent composite dimensions that, like force, are built through multiplication or division of the fundamental ones.

In general, a composite dimension is the product of powers of fundamental dimensions. [1] If we were going to represent these powers for manipulation at runtime, we could use an array of seven ints, with each position in the array holding the power of a different fundamental dimension:

    typedef int dimension[7]; // m  l  t  ...
    dimension const mass      = {1, 0, 0, 0, 0, 0, 0};
    dimension const length    = {0, 1, 0, 0, 0, 0, 0};
    dimension const time      = {0, 0, 1, 0, 0, 0, 0};
    ...

In that representation, force would be:

    dimension const force = {1, 1, -2, 0, 0, 0, 0};

that is, mlt -2. However, if we want to get dimensions into the type system, these arrays won't do the trick: they're all the same type! Instead, we need types that themselves represent sequences of numbers, so that two masses have the same type and a mass is a different type from a length.

Fortunately, the MPL provides us with a collection of type sequences. For example, we can build a sequence of the built-in signed integral types this way:

    #include <boost/mpl/vector.hpp>

    typedef boost::mpl::vector<
         signed char, short, int, long> signed_types;

How can we use a type sequence to represent numbers? Just as numerical metafunctions pass and return wrapper types having a nested ::value, so numerical sequences are really sequences of wrapper types (another example of polymorphism). To make this sort of thing easier, MPL supplies the int_<N> class template, which presents its integral argument as a nested ::value:

    #include <boost/mpl/int.hpp>

    namespace mpl = boost::mpl; // namespace alias
    static int const five = mpl::int_<5>::value;

In fact, the library contains a whole suite of integral constant wrappers such as long_ and bool_, each one wrapping a different type of integral constant within a class template.

Now we can build our fundamental dimensions:

    typedef mpl::vector<
       mpl::int_<1>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>
     , mpl::int_<0>, mpl::int_<0>, mpl::int_<0>
    > mass;

    typedef mpl::vector<
       mpl::int_<0>, mpl::int_<1>, mpl::int_<0>, mpl::int_<0>
     , mpl::int_<0>, mpl::int_<0>, mpl::int_<0>
    > length;
    ...

Whew! That's going to get tiring pretty quickly. Worse, it's hard to read and verify: The essential information, the powers of each fundamental dimension, is buried in repetitive syntactic "noise." Accordingly, MPL supplies integral sequence wrappers that allow us to write:

    #include <boost/mpl/vector_c.hpp>

    typedef mpl::vector_c<int,1,0,0,0,0,0,0> mass;
    typedef mpl::vector_c<int,0,1,0,0,0,0,0> length;  // or position
    typedef mpl::vector_c<int,0,0,1,0,0,0,0> time;
    typedef mpl::vector_c<int,0,0,0,1,0,0,0> charge;
    typedef mpl::vector_c<int,0,0,0,0,1,0,0> temperature;
    typedef mpl::vector_c<int,0,0,0,0,0,1,0> intensity;
    typedef mpl::vector_c<int,0,0,0,0,0,0,1> angle;

Even though they have different types, you can think of these mpl::vector_c specializations as being equivalent to the more verbose versions above that use mpl::vector.

If we want, we can also define a few composite dimensions:

    // base dimension:        m l  t ...
    typedef mpl::vector_c<int,0,1,-1,0,0,0,0> velocity;     // l/t
    typedef mpl::vector_c<int,0,1,-2,0,0,0,0> acceleration; // l/(t2)
    typedef mpl::vector_c<int,1,1,-1,0,0,0,0> momentum;     // ml/t
    typedef mpl::vector_c<int,1,1,-2,0,0,0,0> force;        // ml/(t2)

And, incidentally, the dimensions of scalars (like pi) can be described as:

    typedef mpl::vector_c<int,0,0,0,0,0,0,0> scalar;

3.1.2 Representing Quantities

The types listed above are still pure metadata; to typecheck real computations we'll need to somehow bind them to our runtime data. A simple numeric value wrapper, parameterized on the number type T and on its dimensions, fits the bill:

    template <class T, class Dimensions>
    struct quantity
    {
        explicit quantity(T x)
           : m_value(x)
        {}

        T value() const { return m_value; }
     private:
        T m_value;
    };

Now we have a way to represent numbers associated with dimensions. For instance, we can say:

    quantity<float,length> l( 1.0f );
    quantity<float,mass> m( 2.0f );

Note that Dimensions doesn't appear anywhere in the definition of quantity outside the template parameter list; its only role is to ensure that l and m have different types. Because they do, we cannot make the mistake of assigning a length to a mass:

    m = l;   // compile time type error

3.1.3 Implementing Addition and Subtraction

We can now easily write the rules for addition and subtraction, since the dimensions of the arguments must always match.

    template <class T, class D>
    quantity<T,D>
    operator+(quantity<T,D> x, quantity<T,D> y)
    {
      return quantity<T,D>(x.value() + y.value());
    }

    template <class T, class D>
    quantity<T,D>
    operator-(quantity<T,D> x, quantity<T,D> y)
    {
      return quantity<T,D>(x.value() - y.value());
    }

These operators enable us to write code like:

    quantity<float,length> len1( 1.0f );
    quantity<float,length> len2( 2.0f );

    len1 = len1 + len2;   // OK

but prevent us from trying to add incompatible dimensions:

    len1 = len2 + quantity<float,mass>( 3.7f ); // error

3.1.4 Implementing Multiplication

Multiplication is a bit more complicated than addition and subtraction. So far, the dimensions of the arguments and results have all been identical, but when multiplying, the result will usually have different dimensions from either of the arguments. For multiplication, the relation:

042equ01.jpg

implies that the exponents of the result dimensions should be the sum of corresponding exponents from the argument dimensions. Division is similar, except that the sum is replaced by a difference.

To combine corresponding elements from two sequences, we'll use MPL's transform algorithm. transform is a metafunction that iterates through two input sequences in parallel, passing an element from each sequence to an arbitrary binary metafunction, and placing the result in an output sequence.

    template <class Sequence1, class Sequence2, class BinaryOperation>
    struct transform;  // returns a Sequence

The signature above should look familiar if you're acquainted with the STL transform algorithm that accepts two runtime sequences as inputs:

    template <
        class InputIterator1, class InputIterator2
      , class OutputIterator, class BinaryOperation
    >
    void transform(
        InputIterator1 start1, InputIterator2 finish1
      , InputIterator2 start2
      , OutputIterator result, BinaryOperation func);

Now we just need to pass a BinaryOperation that adds or subtracts in order to multiply or divide dimensions with mpl::transform. If you look through the MPL reference manual, you'll come across plus and minus metafunctions that do just what you'd expect:

    #include <boost/static_assert.hpp>
    #include <boost/mpl/plus.hpp>
    #include <boost/mpl/int.hpp>
    namespace mpl = boost::mpl;

    BOOST_STATIC_ASSERT((
        mpl::plus<
            mpl::int_<2>
          , mpl::int_<3>
        >::type::value == 5
    ));

At this point it might seem as though we have a solution, but we're not quite there yet. A naive attempt to apply the transform algorithm in the implementation of operator* yields a compiler error:

    #include <boost/mpl/transform.hpp>

    template <class T, class D1, class D2>
    quantity<
        T
      , typename mpl::transform<D1,D2,mpl::plus>::type
    >
    operator*(quantity<T,D1> x, quantity<T,D2> y) { ... }

It fails because the protocol says that metafunction arguments must be types, and plus is not a type, but a class template. Somehow we need to make metafunctions like plus fit the metadata mold.

One natural way to introduce polymorphism between metafunctions and metadata is to employ the wrapper idiom that gave us polymorphism between types and integral constants. Instead of a nested integral constant, we can use a class template nested within a metafunction class:

    struct plus_f
    {
        template <class T1, class T2>
        struct apply
        {
           typedef typename mpl::plus<T1,T2>::type type;
        };
    };

Whereas a metafunction is a template but not a type, a metafunction class wraps that template within an ordinary non-templated class, which is a type. Since metafunctions operate on and return types, a metafunction class can be passed as an argument to, or returned from, another metafunction.

Finally, we have a BinaryOperation type that we can pass to transform without causing a compilation error:

    template <class T, class D1, class D2>
    quantity<
        T
      , typename mpl::transform<D1,D2,plus_f>::type // new dimensions
    >
    operator*(quantity<T,D1> x, quantity<T,D2> y)
    {
        typedef typename mpl::transform<D1,D2,plus_f>::type dim;
        return quantity<T,dim>( x.value() * y.value() );
    }

Now, if we want to compute the force exerted by gravity on a five kilogram laptop computer, that's just the acceleration due to gravity (9.8 m/sec2) times the mass of the laptop:

    quantity<float,mass> m(5.0f);
    quantity<float,acceleration> a(9.8f);
    std::cout << "force = " << (m * a).value();

Our operator* multiplies the runtime values (resulting in 6.0f), and our metaprogram code uses transform to sum the meta-sequences of fundamental dimension exponents, so that the result type contains a representation of a new list of exponents, something like:

    vector_c<int,1,1,-2,0,0,0,0>

However, if we try to write:

    quantity<float,force> f = m * a;

we'll run into a little problem. Although the result of m * a does indeed represent a force with exponents of mass, length, and time 1, 1, and -2 respectively, the type returned by transform isn't a specialization of vector_c. Instead, transform works generically on the elements of its inputs and builds a new sequence with the appropriate elements: a type with many of the same sequence properties as vector_c<int,1,1,-2,0,0,0,0>, but with a different C++ type altogether. If you want to see the type's full name, you can try to compile the example yourself and look at the error message, but the exact details aren't important. The point is that force names a different type, so the assignment above will fail.

In order to resolve the problem, we can add an implicit conversion from the multiplication's result type to quantity<float,force>. Since we can't predict the exact types of the dimensions involved in any computation, this conversion will have to be templated, something like:

    template <class T, class Dimensions>
    struct quantity
    {
        // converting constructor
        template <class OtherDimensions>
        quantity(quantity<T,OtherDimensions> const& rhs)
          : m_value(rhs.value())
        {
        }
        ...

Unfortunately, such a general conversion undermines our whole purpose, allowing nonsense such as:

    // Should yield a force, not a mass!
    quantity<float,mass> bogus = m * a;

We can correct that problem using another MPL algorithm, equal, which tests that two sequences have the same elements:

    template <class OtherDimensions>
    quantity(quantity<T,OtherDimensions> const& rhs)
      : m_value(rhs.value())
    {
      BOOST_STATIC_ASSERT((
         mpl::equal<Dimensions,OtherDimensions>::type::value
      ));
    }

Now, if the dimensions of the two quantities fail to match, the assertion will cause a compilation error.

3.1.5 Implementing Division

Division is similar to multiplication, but instead of adding exponents, we must subtract them. Rather than writing out a near duplicate of plus_f, we can use the following trick to make minus_f much simpler:

    struct minus_f
    {
        template <class T1, class T2>
        struct apply
          : mpl::minus<T1,T2> {};
    };

Here minus_f::apply uses inheritance to expose the nested type of its base class, mpl::minus, so we don't have to write:

    typedef typename ...::type type

We don't have to write typename here (in fact, it would be illegal), because the compiler knows that dependent names in apply's initializer list must be base classes. [2] This powerful simplification is known as metafunction forwarding; we'll apply it often as the book goes on. [3]

Syntactic tricks notwithstanding, writing trivial classes to wrap existing metafunctions is going to get boring pretty quickly. Even though the definition of minus_f was far less verbose than that of plus_f, it's still an awful lot to type. Fortunately, MPL gives us a much simpler way to pass metafunctions around. Instead of building a whole metafunction class, we can invoke transform this way:

    typename mpl::transform<D1,D2, mpl::minus<_1,_2> >::type

Those funny looking arguments (_1 and _2) are known as placeholders, and they signify that when the transform's BinaryOperation is invoked, its first and second arguments will be passed on to minus in the positions indicated by _1 and _2, respectively. The whole type mpl::minus<_1,_2> is known as a placeholder expression.

Here's our division operator written using placeholder expressions:

    template <class T, class D1, class D2>
    quantity<
        T
      , typename mpl::transform<D1,D2,mpl::minus<_1,_2> >::type
    >
    operator/(quantity<T,D1> x, quantity<T,D2> y)
    {
       typedef typename
         mpl::transform<D1,D2,mpl::minus<_1,_2> >::type dim;

       return quantity<T,dim>( x.value() / y.value() );
    }

This code is considerably simpler. We can simplify it even further by factoring the code that calculates the new dimensions into its own metafunction:

    template <class D1, class D2>
    struct divide_dimensions
      : mpl::transform<D1,D2,mpl::minus<_1,_2> > // forwarding again
    {};

    template <class T, class D1, class D2>
    quantity<T, typename divide_dimensions<D1,D2>::type>
    operator/(quantity<T,D1> x, quantity<T,D2> y)
    {
       return quantity<T, typename divide_dimensions<D1,D2>::type>(
          x.value() / y.value());
    }

Now we can verify our "force-on-a-laptop" computation by reversing it, as follows:

    quantity<float,mass> m2 = f/a;
    float rounding_error = std::abs((m2 - m).value());

If we got everything right, rounding_error should be very close to zero. These are boring calculations, but they're just the sort of thing that could ruin a whole program (or worse) if you got them wrong. If we had written a/f instead of f/a, there would have been a compilation error, preventing a mistake from propagating throughout our program.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020