MPL: Lazy Evaluation and Nullary Metafunction
Hi All,
I am reading about the lazy evaluation and nullary metafunction in the
MPL book. While discussing the lazy evaluation technique the
following code is presented (page 57):
struct add_pointer_f
{
template <class T>
struct apply : boost::add_pointer<T> {};
};
typedef mpl::vector
AMDG Missing Rainbow wrote:
I am reading about the lazy evaluation and nullary metafunction in the MPL book. While discussing the lazy evaluation technique the following code is presented (page 57):
struct add_pointer_f { template <class T> struct apply : boost::add_pointer<T> {}; }; typedef mpl::vector
seq; typedef mpl::transform > calc_ptr_seq; The point of the above code was to demonstrate that the nullary metafunction calc_ptr_seq represents the sequence "int*, char**, double&*", even though the last element in that sequence is not valid. Thanks to lazy evaluation. As long as we don't access the third element in the sequence represented by calc_ptr_seq, everything should work fine. Is my understanding correct?
Not quite. transform is greedy. When you access calc_ptr_seq::type,
all the elements will be computed immediately. The point is that
if you don't access the ::type, transform will not be instantiated at all.
Actually, this example makes no sense to me.
boost::add_pointer is smart. For example, while double&* is
illegal, boost::add_pointer
Also, other than nullary metafunction, is there any other approach to lazy evaluation? Or will lazy evaluation _always_ be implemented with the help of nullary metafunction?
Well, there are other ways to achieve the same effect, but in MPL, if you have a metafunction that you want to evaluate lazily, you will always do so by leaving off the type. This of course yields a nullary metafunction. In Christ, Steven Watanabe
participants (2)
-
Missing Rainbow
-
Steven Watanabe