Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2008-07-20 11:36:54


on Sun Jul 20 2008, Zeljko Vrba <zvrba-AT-ifi.uio.no> wrote:

> On Sun, Jul 20, 2008 at 09:38:58AM -0400, David Abrahams wrote:
>>
>> "C++ Template Metaprogramming" is not intended to be a reference book
>> about manipulating typelists at all. Did you start reading from the
>> beginning of the book, or did you skip to chapter 3 or so? The
>> introduction lays some important conceptual foundations about what
>> metaprogramming is all about. Typelists (not even a term we use in the
>> book) are merely one means to reaching the many ends of metaprogramming.
>>
> Yes, I did start reading from the beginning. Actually, I have it with
> me. There's "1.6. When Metaprogramming". According to that section,
> the only thing that I'd like to take advantage of is static type
> checking.

Sounds like it's not for you... yet. One day you will begin to
appreciate the value of abstraction with performance. Come back and see
us then :-)

> Section 2 has been understandable, though a bunch of reference
> documentation left me wondering about what all those type
> classification functions might be useful for. The "flyswapper"
> example.. nice and understandable,

So you no longer wonder?

> but not something that has really motivated *me* because it looks like
> a case of invoking a lot of arcane syntax (and I guess it'd have been
> a lot worse without MPL) to do premature optimization.

Optimizing iter_swap is hardly premature. It is a fundamental operation
that governs the efficiency of many algorithms in the standard library.

>> That's a reasonable point of view to take if you don't want to do
>> template metaprogramming and just want to be a consumer of its
>> benefits.
>>
> Well, I *do not know* whether I want to do template metaprogramming in
> the sense as in your book. This was the starting point for this whole
> discussion. I coded a van-Emde Boas tree via template metaprogramming
> (simple recursion), and that has had little to do with types.

What for?

> I have no idea where and if the MPL could have helped -- most probably
> not because the code is similar to the binary number example.

As you probably know, when using MPL one wraps all numeric values inside
types so they can be treated polymorphically (e.g. stored in
containers); if you're doing purely numeric stuff with little program
logic you might find it's easier not to use MPL.

>> Did you look at the binary number example in the first pages of our
>> book? It addresses many of the above questions.
>>
> Yes, it was a nice and understandable example which, coincidentally,
> does not use MPL!

Well, it's not a coincidence. Doing stuff with MPL requires a small
amount of background, which we get to in the following chapters. The
upshot, though, is that MPL is a library that makes it much easier to do
much more sophisticated things that fall into the same category.

If you want to see one of those, you might look at the state machine
example in chapter 11.

> So in chapter 1 you begin nice and easy with an understandable example
> coded without any MPL machinery, and then you suddenly switch in
> chapter 2 to example with type classification.
>
> By the way, how would the binary number example be coded with MPL?
> Would it be sensible to do it at all?

Probably not. I mean, you could do it, but it would likely add quite a
bit of needless syntax.

>> Ah. Well, some people think every application programmer should think
>> of himself as a library developer. If you're writing code that will be
>> re-used by your colleagues, or even by yourself, that code is
>> essentially library code.
>>
> Indeed. "Reused". QT is also reusable, yet it does not use templates heavily.

Yes, but its use of runtime polymorphism incurs a fairly large
abstraction penalty.

>> However, you do have a point: fusion, MPL, and templates in general
>> are useful for building flexible, generalized components that can be
>> configured for different applications without loss of efficiency. If
>> you are building one-off code, you probably don't have much use for
>> them.
>>
> OK, thanks. Anyway, I admire yours and Joels (and others') efforts,
> but.. let's declare 1:0 in favor of MPL and close the discussion.

Was there a contest? Did I miss something?

> I'll steer clear of MPL/fusion for a while more. Maybe the time has
> come for me to switch to Java, JITs are steadily getting better :-)

Ohhh... don't do that. Or at least download and read Stepanov's
"elements of programming" slides before you do. :-)

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net