I recently purchased David Abrahams' and Aleksey Gurtovoy's new book
on the MPL, "C++ Template Metaprogramming" (obviously recently since
it's only been out a couple of weeks), and am working through some of
the examples. I'm having trouble with the factorial example in section
8.3.1, page 160:
#include
#include
#include
#include
#include
#include
namespace mpl = boost::mpl;
template <class N>
struct factorial
: mpl::eval_if<
mpl::equal_to > // check N == 0
, mpl::int_<1> // 0! == 1
, mpl::multiplies< // N! == N * (N-1)!
N
, factorial
>
>
{
BOOST_STATIC_ASSERT(N::value >= 0); // for nonnegative N
};
There's a minor issue in that equal.hpp is (apparently) mistakenly
included instead of equal_to.hpp, but even after fixing that, I can't
get this to compile in the form in which it's written. Should it, or
is it broken?
I'm using gcc 3.3-23 on Linux. The first meaningful line in the error
novel I get is:
error: no type named `tag' in `struct factorial >'
I was able to get a modified version to work, but only by explicitly
invoking the eval_if metafunction and wrapping the result in a constant
wrapper, like so:
template <class N>
struct factorial
: mpl::int_<
mpl::eval_if<
mpl::equal_to >
, mpl::int_<1>
, mpl::multiplies<
N
, factorial
>
>::type::value
>
{
};
But as this is a less-than-trivial departure from the book version, I
don't feel comfortable moving on without first gaining a better
understanding of what's going wrong.
Thanks in advance to anyone who can offer some insight.