473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Expression Templates

Hi,

I came across a paper by Angelika Langer in C++ Users Journal on
Expression Templates.

In the article she had mentioned that the code snippet below used to
calculate a dot product is an expression template.

template <size_t N, class T>
class DotProduct {
public:
static T eval(T* a, T* b)
{ return DotProduct<1,T>::eval(a,b)
+ DotProduct<N-1,T>::eval(a+1,b+1);
}
};

//specialization
template <class T>
class DotProduct<1,T{
public:
static T eval(T* a, T* b)
{ return (*a)*(*b); }
};

template <size_t N, class T>
inline T dot(T* a, T* b)
{ return DotProduct<N,T>::eval(a,b); }
int a[4] = {1,100,0,-1};
int b[4] = {2,2,2,2};
cout << dot<4>(a,b);

Is this actually an expression template or this just a case of
template meta programming since this doesn't overload the function
operator. My understanding of expression templates may be a bit hazy
as I don't have access to books on the topic as of now. Would be glad
if someone here can throw more light on this.

Cheers
Madhusudhan
Jul 31 '08 #1
2 2426
On Jul 31, 1:04 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
madhu.srikka...@gmail.com a écrit :
I came across a paper by Angelika Langer in C++ Users Journal on
Expression Templates.
In the article she had mentioned that the code snippet below
used to calculate a dot product is an expression template.
[...]
Is this actually an expression template or this just a case
of template meta programming since this doesn't overload the
function operator.
IMO template meta programming deals broadly with operations on
type while expression template focuses on template to provide
faster and more expressive code.
I'm not sure, but I'd generally consider expression templates as
a subset of template metaprogramming. Generally, I'd consider
anything which involves "writing code for the compiler" in
templates as template meta-programming.

But it really is a question of vocabulary, and I don't think
that there's any one correct answer.

(Note too that much of what is now known under the name of
expression templates was being done before templates existed,
with inheritance. Since all of the derived types ended up being
used as temporary objects, on the stack, the compiler always
knew the exact type, and could inline the functions anyway, even
though they were virtual.)
Code above would have been also written:
template <class T>
inline dot(int site,T* a, T* b)
{
T ret=T();
for(int i=0;i<size;++i)
{
ret+=a[i]*b[i];
}
}
cout << dot(4,a,b);
Unless you have some aggressive optimization (inlining, loop
unrolling ...), the code is more likely to create temporaries
and other sub-optimal constructs.
Well... maybe not for this simple example:-). But as soon as
you start doing more complex things, yes. You save a lot of
intermediate temporaries, and it's very, very difficult for the
compiler to eliminate them itself.
And the alternative:
cout<< (a[0]*b[0]+...+a[3]*b[3])<<endl;
is cumbersome and doesn't give the meaning of what you do.
And becomes impossible as soon as the dimension ceases to be a
compile time constant.
Using dot<4>(a,b) expand nicely to a[0]*b[0]+...+a[3]*b[3] while
retaining the meaning.
My understanding of expression templates may be a bit hazy
as I don't have access to books on the topic as of now.
Would be glad if someone here can throw more light on this.
http://en.wikibooks.org/wiki/More_C%...ssion-template
Wouldn't it be better to refer him to some original sources,
like Todd Veldhuizen's original article
(http://ubiety.uwaterloo.ca/~tveldhui...ion-Templates/
exprtmpl.html).
There's also considerable treatment in the Vandevoorde and
Josuttis.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 31 '08 #2
Michael DOUBEZ wrote:
Using dot<4>(a,b) expand nicely to a[0]*b[0]+...+a[3]*b[3] while
retaining the meaning.
See my dot alg for better solution. :-)

http://groups.google.com.tj/group/co...f1c607fec4c10b

see asm at end of thread

regards
Andy Little
Jul 31 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KJ | last post by:
My xslt has xsl:templates that match a large majority of the xml in our xml files. What I would like to do is create a test expression that matches any of the tags or text that these templates...
8
by: bartek | last post by:
Hello, I've been toying around with expression templates recently while writing a simple static lexical analyser, hence I have some questions which keep bothering me... The expression objects...
1
by: Michael Lehn | last post by:
I am interested in using expression templates techniques for my container classes. On the web I found PETE: http://acts.nersc.gov/pete/main.html However the download links seems to be broken...
1
by: PengYu.UT | last post by:
Hi, I read Klaus Kreft & Angelika Langer's C++ Expression Templates: An Introduction to the Principles of Expression Templates at...
0
by: norton | last post by:
Hi All, I am learning Regular Expression and currently i am trying to capture information from web page. I wrote the following code to capture the ID as well as the Title Dim regex = New...
2
by: Martin Gernhard | last post by:
Hi, I'm trying to use expression templates to calculate values at compile time. Doing it with just one parameter is no problem: ///Begin Listing 1 #include <iostream> using namespace std; ...
2
by: Zoran Stipanicev | last post by:
I'm trying to port simple expression template to generics but I get some strange errors. The code is shown below and the errors are: (1) 'l_' : is not a member of 'IExpresion' (2) syntax error :...
6
by: Zoran Stipanicev | last post by:
Hi! I've changed the code to use Apply instead of operator() and now I get this errors: (1) left of '.Apply' must have class/struct/union (2) left of '.GetRowNum' must have class/struct/union...
29
by: Dexter | last post by:
This Java based utility may be invoked from Java code to parse mathematical expressions. It is useful for programmers developing calculators, graphing utilities or other math related programs. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.