M
madhu.srikkanth
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
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