[mpl] C++ Template Metaprogramming book - Question

Hi,
I've started reading Abrahams' and Gurtovoy's book, since I'm very
interested in boost.mpl. Seems like a great book. I have a question
regarding the 3rd chapter (3.1.4 section).
Eventually, it says that operator* multiplies the runtime values
(resulting in 6.0f) and the code uses transform to sum the meta-seq.
template

"Kobi Cohen-Arazi"
Hi,
I've started reading Abrahams' and Gurtovoy's book, since I'm very interested in boost.mpl. Seems like a great book. I have a question regarding the 3rd chapter (3.1.4 section). Eventually, it says that operator* multiplies the runtime values (resulting in 6.0f) and the code uses transform to sum the meta-seq.
template
quantity ::type> operator*(quantity x, quantity y) { typedef mpl::transform ::type dim; return quantity (x.value() * y.value()); } 1. Why passing x and y by value? why not const&
a. It adds extra tokens that distract from the point of the example.
b. T is probably float or double, which is usually fine to pass by
value, and thus quantity
2. How did we got 6f ?
It's an erratum: http://boost-consulting.com/mplbook/errata.html#metafunctions-title
3. How and when the mpl::transform is getting into action here? I cannot spot it.
It gets into action at compile-time, when the operator is instantiated. The compiler has to figure out what type the function returns, and to do so, it instantiates the transform template to discover what its nested ::type is. HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Kobi Cohen-Arazi