Boost logo

Boost :

From: David A. Greene (greened_at_[hidden])
Date: 2001-12-11 15:18:06


David Abrahams wrote:

> I think the "quoted" name hurts the understandability of the idiom for
> anyone but a lisp programmer. There are technical reasons apart from
> broken compilers to prefer this idiom, but I'll let Aleksey address
> them.

I'm certainly not a Lisp programmer, so that absolutely could
be the case. As I said, I'm approaching this from a novice
standpoint. I'd like to hear about the other technical reasons.
I have _Generative_Programming_ but haven't got to that point
in the book yet. :)

>>By this point I was almost totally dumbfounded. That's a lot of
>>ugly code to do a conceptually very simple thing. Andrei is right.
>>Template metaprogramming is more naturally expressed using recursion
>>because templates define a functional language. I'm reminded of the
>>ugly transformations from recursion to iteration, or explicity
>>coding a parser value stack rather than using inherited and
>>synthesized attributes. Yes, it's possible, but why do it when it
>>is often much simpler to let things "fall out" naturally? Less
>>code, fewer bugs.
>
> I think you're drawing conclusions from things unrelated to the
> conclusions themselves.

Err..urm...huh? What things?

> People have the same complaints about the STL. We all know that the
> use of lots of composers and binders can make code hard to read, and
> many of us wouldn't write that way for runtime code.

That's exactly what I was getting at, though I didn't state it
explicitly. We don't like STL code that looks messy, so why
should MPL code be messy?

> Let's see if we can rewrite Mat's example more simply, if in more
> characters:

Good, this is a useful exercise.

> // Now we can define GenScatterHierarchy
>
> template <class TList, class Transformation>
> struct GenScatterHierarchy {
> typedef boost::mpl::for_each<
> TList, EmptyType, inherit_transformed<Transformation>
> >::state type;
> };

This is certainly much more readable. "Aggregating" the
pieces-parts into inherit_transformed improved my understanding
greatly. Still, a looping construct to generate trees seems
a little "out of place." For me, recursion is just much more
natural. Perhaps this isn't true for most.

> I find the analogy between the use of the STL and the use of MPL makes
> things much easier.

I think it is somewhat deceptive. The names and arguments look
familiar, but the STL algorithms work smoothly because iteration
is a natural interface to containers/sequences. I suppose one
could argue that a typelist is just another sequence, which it
certainly is, but due to the limited interface we have for
C++ metaprogramming (no vararg templates, etc.), recursion
is a natural way to represent them. The simple observation
that (partial) specialization is used to provide a terminator/
sentinel points to the natural use of recursion in
metaprogramming.

The STL algorithms break down somewhat with the associative
containers. Their recursive structure makes std::find (as
an example) a non-ideal tool. Thus we have the member find
which (in my implementation) does not mention iterators until
setting up the return value (pity that it is not implemented
with recursion :)). Use the right tool for the job, as
always. I'm not arguing that iteration isn't useful for
metaprogramming. I just take issue with it being presented
as the universal way of doing things.

This is all just IMHO, of course. These are first impressions.

> Composers and binders become really useful when you have lots of
> little metafunctions lying around for doing important jobs in your
> domain. At that point, you want to be able to re-use and combine
> them.

Of course composers and binders are useful. But they have their
limitations. Perhaps a few more typedefs in Mat's example would
have helped.

> Of course, a compile-time lambda facility (which has already been
> started in MPL) could be much nicer.

It's tough to disagree with that! :)

                                 -Dave

-- 
"Some little people have music in them, but Fats, he was all music,
   and you know how big he was."  --  James P. Johnson

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk