Boost logo

Boost :

From: Frank Laub (fried_at_[hidden])
Date: 2004-11-17 04:24:26

Vesa Karvonen wrote:
> Lazy Metaprogramming Library
> ============================
> Vesa Karvonen
> vesa_karvonen_at_[hidden]
> 30. October 2004
This is awesome!

I have been working on something very similar to this; looks like I'm
not alone in the lazy evaluation idea. My library goes a bit farther
than proof-of-concept by actually trying to use it in a real-world case.
      I have run into a significant problem however: the template
nesting limitation. I have not yet seen a way to make Lazy TMP scale
for any real problems.

Say we have a list:

typedef cons< A, cons< B, cons <C, nil > > > list_t;

In this case, we have 3 cons cells. I can do operations on it like:

typedef nth< I<1>, list_t > nth_result;

When nth_result is evaulated, the resulting type will be B.
typedef nth_result::eval nth_eval; // nth_eval is B

But nth is a fairly trival function:

#define FUN(n, _name) \
        template<BOOST_PP_ENUM_PARAMS(n, typename _p) > \
        struct _name {
#define BEGIN typedef typename
#define END ::eval eval; };

// _p0 = index
// _p1 = {list}
FUN(2, nth)
        if_< _p1,
                if_< equals< _p0, I<0> >,
                        car< _p1 >,
                                sub< L2< _p0, I<1> > >,
                                cdr< _p1 >

Yes, the above code is actually C++! The magic of lazy evaulation lets
us write such elegant stuff.

The thing is, even with this little example, the compiler will have to
slog though many many nested types (espically brutal is the car and
cdr). This usually results in the compiler running out of memory pretty
quickly for lists that are only 10 elements.

I've looked at your approach and it looks promising, but I'm not sure it
will aleviate this problem. Is there something special about
inheritance that makes the compiler happier in that it doesn't eat up
its memory as fast? I'm starting to think that C++ was never really
meant to do this kind of thing, and therefore is a lost cause?

BTW, I'm cleaning up my library for others to look at, but I can send it
to you in its current state if you don't mind the mess. It works for
VC7.1 and (believe it or not) EVC4. I'm sure with a bit of tweaking it
will work fine on g++; the hard part was working around partial and full
specializations not working right on evc4.

Frank Laub

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