Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-16 18:35:48


Douglas Gregor wrote:

> Boost.Function can eliminate the need for the template parameters:
> typedef boost::function<void> Action;
> typedef boost::function<bool> Pred;

> A library like Lambda, Boost.Bind, or FACT would let you define the predicate
> and action inline.

        In general, it cannot be done.
If you consider, say:

        x = x + 1

you can wrap it using something like

        ASSIGN ( VAR(&x), SUM ( VAR(&x), VAL (1) )

[the actual tempate code is probably grosser :-[

but such a decomposition only works for some constructions.
Other constructions cannot be wrapped. For example,
functions with n arguments, where n exceeds the number
supported by the library. Struct member access,
pointer to member formation, exception handling,
multiple inheritance ... the list is long.

In the end, you're not writing C++, but a new language,
and you're much better off (IMHO) using a real programming
language like Ocaml (generate efficient native code),
or using a code generator like Felix (generate C++
for seamless binding to existing code).

Or just sticking to plain C++, and writing everything
by hand instead of using generics.

FYI: I was writing a book on STL some years ago,
and I gave up because, while it is _possible_ to do quite
significant work using templates, the result
is so gross as to be useless. You can replace
a loop

        for(;;) { body }

with

        for_each(body_functoid)

but then you have to translate the body, and in the end
the first form is easier to write, gives better
diagnostics, compiles faster, generates better code,
etc etc .. in the end, template metaprogramming
is only interesting if you're considering how to
fix the core language to actually make it useful.

The killer was a triple loop: three or four lines
of C++ turned into several _pages_ of templates.
I used only plain STL, so I guess you could do better
with more combinators (eg using Lambda or Bind,
or whatever), but in the end, these library cannot
solve the general problem, only slightly expand
the class of problems which can be solved.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

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