Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Front Page / Tutorial: Metafunctions and Higher-Order Metaprogramming / Exercises

Exercises

1-0.
Use BOOST_STATIC_ASSERT to add error checking to the binary template presented in section 1.4.1 so that binary<N>::value causes a compilation error if N contains digits other than 0 or 1.
1-1.
Turn vector_c<int,1,2,3> into a type sequence with elements (2,3,4) using transform.
1-2.
Turn vector_c<int,1,2,3> into a type sequence with elements (1,4,9) using transform.
1-3.
Turn T into T**** by using twice twice.
1-4.
Turn T into T**** using twice on itself.
1-5.

There's still a problem with the dimensional analysis code in section 1.1. Hint: What happens when you do:

f = f + m * a;

Repair this example using techniques shown in this chapter.

1-6.
Build a lambda expression that has functionality equivalent to twice. Hint: mpl::apply is a metafunction!
1-7*.

What do you think would be the semantics of the following constructs:

typedef mpl::lambda<mpl::lambda<_1> >::type t1;
typedef mpl::apply<_1,mpl::plus<_1,_2> >::type t2;
typedef mpl::apply<_1,std::vector<int> >::type t3;
typedef mpl::apply<_1,std::vector<_1> >::type t4;
typedef mpl::apply<mpl::lambda<_1>,std::vector<int> >::type t5;
typedef mpl::apply<mpl::lambda<_1>,std::vector<_1> >::type t6;
typedef mpl::apply<mpl::lambda<_1>,mpl::plus<_1,_2> >::type t7;
typedef mpl::apply<_1,mpl::lambda< mpl::plus<_1,_2> > >::type t8;

Show the steps used to arrive at your answers and write tests verifying your assumptions. Did the library behavior match your reasoning? If not, analyze the failed tests to discover the actual expression semantics. Explain why your assumptions were different, what behavior you find more coherent, and why.

1-8*.
Our dimensional analysis framework dealt with dimensions, but it entirely ignored the issue of units. A length can be represented in inches, feet, or meters. A force can be represented in newtons or in kg m/sec2. Add the ability to specify units and test your code. Try to make your interface as syntactically friendly as possible for the user.