Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-06-26 10:08:09

On Wednesday 26 June 2002 10:09 am, Douglas Gregor wrote:
> We'll see. Using MPL's placeholders there are a few areas where the variant
> code size could be reduced (for instance, there should be no need for
> details::add_const or details::max_size)

Some examples:

The computation of 'max_size' in the variant class used to use 26 lines of
code in two auxiliary classes (details::max_size and
details::sizeof_comparison). Instead, just use:

          max_size = sizeof(typename mpl::max_element<
                                        mpl::size_of<mpl::_2> >

Similarly, the computation of the alignment type 'align' was 33 lines, but can
be written as:

          max_align = (alignment_of<
                         typename mpl::max_element<
                                     details::align_of<mpl::_2> >

        typedef typename type_with_alignment<max_align>::type align;

The align_of template is a simple wrapper around the alignment_of template in
the type traits library; I expect the need for this wrapper would disappear
in future versions if MPL is accepted. align_of looks like this:

        template<typename T>
        struct align_of : public alignment_of<T>
          typedef mpl::integral_c<std::size_t,value> type;

Finally, the 'is_not_convertible_from' metafunction is also unnecessary. We
can use placeholders instead. So the metafunction object
is_not_convertible_from<T> is just:

  mpl::logical_not<is_convertible<T, mpl::_1> >

That saves another 11 lines.

Two comments:
  1) I don't think the MPL lambda facilities work under MSVC :(
  2) I had to make two bugfixes in MPL: neither apply_if nor max_element were
applying mpl::lambda to their predicates. They need to.

Just for a quick comparison, here's the max_align calculation with type lists:

template<typename List> struct get_max_align;

template<typename H, typename T>
struct get_max_align<cons<H, T> >
  enum { a1 = alignment_of<H>::value };
  enum { a2 = get_max_align<T>::align };
  enum { align = a1 > a2? a1 : a2 };

struct get_max_align<nil>
  enum { align = 1 };

13 lines for the typelist version vs. 7 lines for the MPL version. The
important question is: which is more readable?


Boost list run by bdawes at, gregod at, cpdaniel at, john at