Hi all,
As some book tells, I try the following example of expression
template.
template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i)
{
return Op::apply(lod[i], rod[i]);
}
};
struct Plus
{
static double apply( double a, double b) { return a+b; }
};
template <typename LeftOpd>
LOP< LeftOpd, Plus, Vec > operator+(LeftOpd a, Vec b)
{
return LOP<LeftOpd, Plus, Vec>(a, b);
};
It is a *very* simple example. I am trying to modify it. I have
written my vector class (a template class, says MyVec<T>). So here
Plus and opeator+ should also be modified to a template structure and
template function. I try the following code(I am sorry, I really have
no idea how to do it)
template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod[i], rod[i]);
}
};
template <typename ADT>
struct Plus
{
static ADT apply( ADT a, ADT b) { return a+b; }
};
template <typename LeftOpd, typename ADT>
LOP< LeftOpd, Plus<ADT>, MyVec<ADT> > operator+(LeftOpd a, MyVec<ADT>
b)
{
return LOP<LeftOpd, Plus<ADT>, MyVec<ADT> >(a, b);
};
The code don't work. I am looking for your help.
BTW, I have also defined my Matrix class. I would like to generalize
the expression template. For instance, I hope the following code works
with the help of expression template
MyMat<double> M(10,10);
MyVec<double> V(10); // a column vector
(M*V + 2.3*M)*M
Any idea? 4 2009
"Rex_chaos" <re*******@21cn.com> wrote in message
news:f7*************************@posting.google.co m... [...] For instance, I hope the following code works with the help of expression template
MyMat<double> M(10,10); MyVec<double> V(10); // a column vector
(M*V + 2.3*M)*M
Any idea?
Yes. Take a look at Blitz++.
Dave
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
On 9 Oct 2003 21:43:29 -0700, re*******@21cn.com (Rex_chaos) wrote: Hi all, As some book tells, I try the following example of expression template.
template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i) { return Op::apply(lod[i], rod[i]); } };
struct Plus { static double apply( double a, double b) { return a+b; } };
template <typename LeftOpd> LOP< LeftOpd, Plus, Vec > operator+(LeftOpd a, Vec b) { return LOP<LeftOpd, Plus, Vec>(a, b); };
It is a *very* simple example. I am trying to modify it. I have written my vector class (a template class, says MyVec<T>). So here Plus and opeator+ should also be modified to a template structure and template function. I try the following code(I am sorry, I really have no idea how to do it)
template < typename LeftOpd, typename Op<typename ADT>, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i) { return Op<ADT>::apply(lod[i], rod[i]); } };
That class shouldn't have changed at all. It was already a general
vector binary operation class. The syntax you've used is illegal in
any case (I think you're trying to make Op a template template
parameter, but there's no need anyway).
template <typename ADT> struct Plus { static ADT apply( ADT a, ADT b) { return a+b; } };
That's fine. You should probably add a typedef for ADT into the struct
too.
template <typename LeftOpd, typename ADT> LOP< LeftOpd, Plus<ADT>, MyVec<ADT> > operator+(LeftOpd a, MyVec<ADT> b) { return LOP<LeftOpd, Plus<ADT>, MyVec<ADT> >(a, b); };
That looks ok to me, although you are passing everything by value,
which is a no-no for vectors. I think you need a class that acts as a
reference to a vector.
The code don't work. I am looking for your help.
BTW, I have also defined my Matrix class. I would like to generalize the expression template. For instance, I hope the following code works with the help of expression template
MyMat<double> M(10,10); MyVec<double> V(10); // a column vector
(M*V + 2.3*M)*M
Any idea?
Getting that to work will require a lot of code. There are various
expression template libraries out there, such as Blitz++ and PETE http://www.codesourcery.com/pooma/pete. See also www.oonumerics.org.
You should be able to find various papers on the subject which might
help you. However, I suspect you've started from Todd Veldhuizen's
paper in any case.
Tom
> > template < typename LeftOpd, typename Op<typename ADT>, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i) { return Op<ADT>::apply(lod[i], rod[i]); } };
That class shouldn't have changed at all. It was already a general vector binary operation class. The syntax you've used is illegal in any case (I think you're trying to make Op a template template parameter, but there's no need anyway).
Here is a problem. If I don't modifiy the class, how can I tell
LOP::apply to return a general type (i.e. ADT)? template <typename ADT> struct Plus { static ADT apply( ADT a, ADT b) { return a+b; } };
That's fine. You should probably add a typedef for ADT into the struct too.
What do you mean by 'add a typedef for ADT'? Do you mean it's no need
to build the struct as a template here?
On 17 Oct 2003 21:20:30 -0700, re*******@21cn.com (Rex_chaos) wrote: > >template < typename LeftOpd, typename Op<typename ADT>, typename >RightOpd > >struct LOP >{ > LeftOpd lod; > RightOpd rod; > > LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {} > > ADT operator[](const unsigned int i) > { > return Op<ADT>::apply(lod[i], rod[i]); > } >}; That class shouldn't have changed at all. It was already a general vector binary operation class. The syntax you've used is illegal in any case (I think you're trying to make Op a template template parameter, but there's no need anyway).
Here is a problem. If I don't modifiy the class, how can I tell LOP::apply to return a general type (i.e. ADT)?
If you add a typedef to your Op classes, then you can do:
template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;
typedef typename Op::ADT ADT;
LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod[i], rod[i]);
}
};
The idea is that the Ops interface includes a typedef for the kind of
ADT that the methods are dealing in. >template <typename ADT> >struct Plus >{ > static ADT apply( ADT a, ADT b) { return a+b; } >};
That's fine. You should probably add a typedef for ADT into the struct too.
What do you mean by 'add a typedef for ADT'? Do you mean it's no need to build the struct as a template here?
template <typename A>
struct Plus
{
typedef A ADT;
static ADT apply( ADT a, ADT b) { return a+b; }
};
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tina Li |
last post by:
Hello,
I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum
recursion limit exceeded". Here is the pattern string:
...
|
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...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However,...
|
by: Steve |
last post by:
Hi All,
I'm having a tough time converting the following regex.compile patterns
into the new re.compile format. There is also a differences in the
regsub.sub() vs. re.sub()
Could anyone lend...
|
by: Marc Gravell |
last post by:
In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if linq
also includes a parser? It just seemed it might be a handy way to...
| |
by: Lawrence Spector |
last post by:
I ran into a problem using g++. Visual Studio 2005 never complained
about this, but with g++ I ran into this error. I can't figure out if
I've done something wrong or if this is a compiler bug. ...
|
by: Dan Smithers |
last post by:
What constitutes a constant-expression? I know that it is something that
can be determined at compile time.
I am trying to use template code and keep getting compiler errors
"error: cannot...
|
by: madhu.srikkanth |
last post by:
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...
|
by: Peng Yu |
last post by:
Hi,
Expression template can be used for the implementation of simple
operators without using temporaries (e.g. the ones in the book C++
Template).
I'm wondering whether expression template is...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |