|
Boost : |
Subject: Re: [boost] proposal for metafunction mpl::eval
From: Abel Sinkovics (abel_at_[hidden])
Date: 2013-09-12 04:17:45
Hi,
On 09/11/13 22:26, klaus triendl wrote:
> Nevertheless I'd like to propose a new metafunction that simply
> evaluates its argument:
>
> template<typename F>
> struct eval: F::type
> {};
>
>
> I've found it somehow useful because it avoids syntactical sugar when
> specifying a condition to be evaluated for eval_if:
>
> eval_if< eval<apply<UnaryF, Arg>>,
> F1, F2
> >;
>
>
> instead of writing:
>
> eval_if< typename apply<UnaryF, Arg>::type,
> F1, F2
> >;
>
>
> Any comments on this?
"apply<UnaryF, Arg>" is an expression in template metaprogramming. By
writing "apply<UnaryF, Arg>::type" you evaluate it. Adding an extra call
to the "eval<...>" metafunction will not evaluate it for you unless
"eval<T>" is an alias of T::type. You could write a version of eval_if
that evaluates its condition for you, so you don't need to do it yourself:
template <class C, class T, class F>
struct lazy_eval_if : eval_if<typename C::type, T, F> {};
Then you could use it in a nice way in your metaprograms:
lazy_eval_if<apply<UnaryF, Arg>, F1, F2>
You can find this lazy_eval_if function in Metamonad (it is called
metamonad::if_): http://abel.web.elte.hu/mpllibs/metamonad/if_.html
Another way of tackling the problem is taking the metaprogram
eval_if<apply<UnaryF, Arg>, F1, F2>
and evaluating it in a lazy way:
metamonad::lazy<
eval_if<
apply<UnaryF, Arg>,
metamonad::lazy_argument<F1>,
metamonad::lazy_argument<F2>
>
>::type
The "lazy<...>" template takes its argument (a metaprogram) and
evaluates every argument of every metafunction call in it. Since eval_if
is a special metafunction (its second and third arguments are lazy), it
needs the extra lazy_argument templates as well.
You can read about it and laziness in metaprograms in the Metamonad user
manual
(http://abel.web.elte.hu/mpllibs/metamonad/manual.html#lazy-metafunctions)
I gave a talk at C++Now this year about Metamonad and the first part of
the talk covered laziness in metaprograms. You can watch it here:
http://www.youtube.com/watch?v=aIj034VCUD8
Regards,
Ábel
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk