[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 <class T, class D1, class D2> quantity<T, typename mpl::transform<D1,D2, plus_f>::type> operator*(quantity<T,D1> x, quantity<T,D2> y) { typedef mpl::transform<D1,D2, plus_f>::type dim; return quantity<T,dim>(x.value() * y.value()); } 1. Why passing x and y by value? why not const& 2. How did we got 6f ? 3. How and when the mpl::transform is getting into action here? I cannot spot it. Thanks -- - Kobi
"Kobi Cohen-Arazi" <kobi.cohenarazi@gmail.com> writes:
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 <class T, class D1, class D2> quantity<T, typename mpl::transform<D1,D2, plus_f>::type> operator*(quantity<T,D1> x, quantity<T,D2> y) { typedef mpl::transform<D1,D2, plus_f>::type dim; return quantity<T,dim>(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<T,...> probably has the same size and is also fine to pass by value
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