Boost logo

Boost Users :

From: Brad Austin (artax_at_[hidden])
Date: 2005-01-22 22:34:56


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 <boost/mpl/int.hpp>
    #include <boost/mpl/multiplies.hpp>
    #include <boost/mpl/equal.hpp>
    #include <boost/mpl/eval_if.hpp>
    #include <boost/mpl/prior.hpp>
    #include <boost/static_assert.hpp>

    namespace mpl = boost::mpl;

    template <class N>
    struct factorial
      : mpl::eval_if<
            mpl::equal_to<N,mpl::int_<0> > // check N == 0
          , mpl::int_<1> // 0! == 1
          , mpl::multiplies< // N! == N * (N-1)!
                N
              , factorial<typename mpl::prior<N>::type>
>
>
    {
        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<mpl_::int_<0> >'

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<N,mpl::int_<0> >
              , mpl::int_<1>
              , mpl::multiplies<
                    N
                  , factorial<typename mpl::prior<N>::type>
>
>::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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net