More complex expression template

P

PengYu.UT

Hi,

I read Klaus Kreft & Angelika Langer's C++ Expression Templates: An
Introduction to the Principles of Expression Templates at
http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm

It provide an express template for only one argument (see Listing 19
and so on), which can be used to do numerical integral.

I'm wondering how to generalize it to handle multiple arguments.

Say, I change the definition of Identity in Listing 19 such that it
have one `int' template argument. Then I have
Identity<1> x;
Identity<2> y;
Identity<3> z;

(x * y) and (y * z) should return two functions each of which accepts 2
arguments. But ((x * y) * (y * z)) should return a function which
accept 3 (not 4) arguments(and the arguments should be in the order of
x, y, z).

If you know anyone have implement this kind of expression template,
would you please share with me. Or if you can give me some hints on how
to define this expression template, it will helpful as well.

Thanks,
Peng


Best wishes,
Peng
 
A

Alf P. Steinbach

* (e-mail address removed):
I read Klaus Kreft & Angelika Langer's C++ Expression Templates: An
Introduction to the Principles of Expression Templates at
http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm

It provide an express template for only one argument (see Listing 19
and so on), which can be used to do numerical integral.

I'm wondering how to generalize it to handle multiple arguments.

Say, I change the definition of Identity in Listing 19 such that it
have one `int' template argument. Then I have
Identity<1> x;
Identity<2> y;
Identity<3> z;

(x * y) and (y * z) should return two functions each of which accepts 2
arguments. But ((x * y) * (y * z)) should return a function which
accept 3 (not 4) arguments(and the arguments should be in the order of
x, y, z).

Personally I'd go for

template< T > T foo( T x, T y, T z ) { return x*y*y*z; }

I think the "readability" of inline expressions, which is their only
reason for existence, is nothing of the sort: a well-chosen function
name conveys much more and more clearly than a cryptic expression.

But see

If you know anyone have implement this kind of expression template,
would you please share with me. Or if you can give me some hints on how
to define this expression template, it will helpful as well.

I remember reading about it. You'll have to do the equivalent of a set
union for the argument types. A bitvector (or that sort of encoding,
limiting the possible argument types) might just be the thing. But the
result would, IMHO, probably just be an expression utility that allows
you to write even more unreadable and cryptic expressions. Now what's
the arity here...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top