C++ Template Metaprogramming
Solutions to the exercises throughout the book
|
Implement a binary to decimal converter as a sequence of binary digits. More...
Files | |
file | exercise-6-1.hpp |
Solution to Exercise 6-1. | |
Namespaces | |
namespace | exercise_6_1 |
Encapsulate solution for Exercise 6-1. | |
Classes | |
struct | boost::mpl::next< exercise_6_1::binary< N > > |
Strip the least-significant binary digit from the number. More... | |
struct | boost::mpl::deref< exercise_6_1::binary< N > > |
Algorithms require the full binary type for computations. More... | |
struct | boost::mpl::begin_impl< exercise_6_1::binary_tag > |
Simply return the binary type as is. More... | |
struct | boost::mpl::end_impl< exercise_6_1::binary_tag > |
When we have a value of 0, there are no more digits. More... | |
Functions | |
static void | anonymous_namespace{chapter-6.cpp}::test_exercise_6_1 () |
Tests for Exercise 6-1. | |
Implement a binary to decimal converter as a sequence of binary digits.
6-1. Rewrite the binary template from section 1.4.1 using one of the MPL sequence iteration algorithms, and write a test program that will only compile if your binary template is working. Compare the amount of code you wrote with the version using handwritten recursion presented in Chapter 1. What characteristics of the problem caused that result?
Compare the amount of code:
This is a BOATLOAD more code.
What characteristics of the problem caused that result?
Really, this solution is only reasonable if one expects to traverse the individual digits in a binary number. Since (as stated) the problem only takes a binary number input and returns a result, constructing the mechanics needed by a sequence algorithm to accumulate a (decimal) result is quite overkill.
|
static |
Tests for Exercise 6-1.
Definition at line 156 of file chapter-6.cpp.