proposal for metafunction mpl::eval
Hi, I'm not so much into meta-programming, so I'm not sure whether there's already an alternative available. 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? -- klaus triendl
On 11 September 2013 21:26, klaus triendl wrote:
Hi,
I'm not so much into meta-programming, so I'm not sure whether there's already an alternative available.
Nevertheless I'd like to propose a new metafunction that simply evaluates its argument:
template<typename F> struct eval: F::type {};
You can only derive from class types, so this can't be used with metafunctions returning scalar types, references, pointers, arrays, or functions. C++11 provides what you want: template<typename F> using eval = typename F::type;
Am Wed, 11 Sep 2013 21:32:00 +0100 schrieb Jonathan Wakely <jwakely.boost@kayari.org>:
On 11 September 2013 21:26, klaus triendl wrote:
Hi,
I'm not so much into meta-programming, so I'm not sure whether there's already an alternative available.
Nevertheless I'd like to propose a new metafunction that simply evaluates its argument:
template<typename F> struct eval: F::type {};
You can only derive from class types, so this can't be used with metafunctions returning scalar types, references, pointers, arrays, or functions.
I'm aware of this, however the implementation could be different: template<typename F> struct eval { typedef typename F::type type; } It's similar to eval_if with respect to evaluation.
C++11 provides what you want:
template<typename F> using eval = typename F::type;
That's cool :) It would be interesting of course to have it available in the mpl library... -- klaus triendl
I'm not so much into meta-programming, so I'm not sure whether there's already an alternative available.
Nevertheless I'd like to propose a new metafunction that simply evaluates its argument:
template<typename F> struct eval: F::type {};
You can only derive from class types, so this can't be used with metafunctions returning scalar types, references, pointers, arrays, or functions.
I'm aware of this, however the implementation could be different:
template<typename F> struct eval { typedef typename F::type type; }
This is now a different metafunction - you have to write typename eval<F>::type to get at the type (rather than just eval<F>), which is no better than typename F::type Regards, Nate
Am Wed, 11 Sep 2013 23:01:04 +0000 schrieb Nathan Ridge <zeratul976@hotmail.com>:
You can only derive from class types, so this can't be used with metafunctions returning scalar types, references, pointers, arrays, or functions.
I'm aware of this, however the implementation could be different:
template<typename F> struct eval { typedef typename F::type type; }
This is now a different metafunction - you have to write
typename eval<F>::type
You're damn right! However I don't see a practical problem here: Regarding my motivating case, eval_if anyway expects an integral constant as its evaluation condition, therefore F must yield an integral constant. Regards, klaus
On Sep 12, 2013 8:15 AM, "klaus triendl" <klaus@triendl.eu> wrote:
Am Wed, 11 Sep 2013 23:01:04 +0000 schrieb Nathan Ridge <zeratul976@hotmail.com>:
You can only derive from class types, so this can't be used with metafunctions returning scalar types, references, pointers, arrays, or functions.
I'm aware of this, however the implementation could be different:
template<typename F> struct eval { typedef typename F::type type; }
This is now a different metafunction - you have to write
typename eval<F>::type
You're damn right!
However I don't see a practical problem here: Regarding my motivating case, eval_if anyway expects an integral constant as its evaluation condition, therefore F must yield an integral constant.
What problem are you trying to solve by adding this metafunction?
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
participants (4)
-
Abel Sinkovics
-
Jonathan Wakely
-
klaus triendl
-
Nathan Ridge