From: David Abrahams (dave_at_[hidden])
Date: 2002-12-30 23:32:03
"Jaap Suter" <J.Suter_at_[hidden]> writes:
> Thanks! All of the code from my original message is now turned into:
> // \brief Meta function, returns the number of bits equal to 1 in a given number.
> // \param N Some number. Concept: boost::mpl::integral_c
> // \retval ::type boost::mpl::integral_c< size_t, Number of bits equal to 1 in N >
> // \warning Don't touch default value;
> // \todo Only works for unsigned types, needs to be fixed;
> template < typename N, typename Result = mpl::integral_c< size_t, 0 > >
> struct num_bits_set
> typedef typename mpl::apply_if<
> typename mpl::equal_to<
> mpl::integral_c< typename N::value_type, 0 >
Hmm, I think you want to drop that. You could use apply_if_c, but why bother?
> mpl::integral_c< size_t, N::value & (N::prior::value) >,
> typename Result::next
> >::type type;
> Definitely a lot shorter.
Nifty! You're using one of my favorite metaprogramming tricks, the
default template parameter which allows you to avoid creating a
separate implementation template. I almost forgot about that one,
it's been so long since I've been able to use it.
> I need to change my algorithms in some other places too I
> think. However, the functional style of programming and the
> associated amount of recursion does not leave much flexibility.
It's usually easier if you re-use the algorithms supplied by the MPL
> I'm doing an N^2 algorithm in a few places which is kinda heavy, so
> I definitely need to fix that. I'll start looking for some function
> programming tutorials that mention conversion from imperative to
> functional style, I'm sure there are a few common idioms to speed
> things up (or reduce recursion).
The loop unrolling technique you mention below can help reduce
recursion, but it won't change "big O" for your algorithm. Its main
purpose is to avoid deeply nested templates, which may anger the
little man in your compiler ;-)
> Also, I'll need to start thinking about
> and http://users.rcn.com/abrahams/instantiation_speed/index.html.
Please let me know if you have questions. I think I wrote both of
> Already, I'm running into compiler error:
> e:\library\boost_1_29_0\boost\mpl\if.hpp(95) : fatal error C1076:
> compiler limit : internal heap limit reached; use /Zm to specify a
> higher limit
> now. I suppose I really need to tweak my algorithms.
No, probably you just need to follow the instructions and specify /Zm
;-) We use a fairly high number by default in the msvc toolset.
> By the way, would it be worthwhile to create an 'Effective MPL' wiki
> page with some common gotchas on the different compilers? We could
> refer to the document on Integral Constant Expressions, we could
> mention the 'early template instantiation' and the work-around, and
> we could mention the lambda complications. And I'm sure that you and
> Dave have more gold nuggets from your experience using the MPL in
> real-world code.
Yes, it would probably be very worthwhile.
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk