Section 9.12.  Exercises
Team LiB
Previous Section Next Section

9.12. Exercises

9-0.

Many compilers contain a "single-inheritance" EBO. That is, they will allocate an empty base at the same address as a data member, but they will never allocate two bases at the same address. On these compilers, our storage implementation is suboptimal for the case where F and G are both empty. Patch storage to avoid this pitfall when NO_MI_EBO is defined in the preprocessor.

9-1.

What happens to our compose template when F and G are the same empty class? How would you fix the problem? Write a test that fails with identical empty F and G, then fix compose_fg so that the test passes.

9-2.

We may not be able to compose arbitrary function objects with compose_fg2, but we can use it to compose statically initialized function objects. (Hint: Review the list at the beginning of section 9.6 of types that can be passed as template arguments). Compile a small program that does so and, if you can read your compiler's assembly-language output, analyze the efficiency of the resulting code.

9-3*.

Write a generalized iterator template that uses type erasure to wrap an arbitrary iterator type and present it with a runtime-polymorphic interface. The template should accept the iterator's value_type as its first parameter and its iterator_category as the second parameter. (Hint 1: Use Boost's iterator_facade template to make writing the iterator easier. Hint 2: You can control whether a given member function is virtual by using structure selection.)

9-4.

Change the sum overload example in section 9.9 so that it can add the arithmetic innermost elements of arbitrarily nested containers such as std::list<std::list<std:: vector<int> > >. Test your changes to show that they work.

9-5.

Revisit the dimensional analysis code in Chapter 3. Instead of using BOOST_STATIC_ASSERT to detect dimension conflicts within operator+ and operator-, apply SFINAE to eliminate inappropriate combinations of parameters from the overload sets for those operators. Compare the error messages you get when misusing operator+ and operator- in both cases.

    Team LiB
    Previous Section Next Section