473,385 Members | 1,655 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,385 software developers and data experts.

'Assignable' Expression Templates?

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 are wrapped around following the
'curious base' pattern.

E.g.

//---------------------------
// BASE class for expressions
//---------------------------
template <typename T>
class Base : public T
{
public:
T& Get()
{
return *static_cast<T*>(this);
}
//...const overload here...
};

//---------------------------
// Item that does the work.
//---------------------------
class Item : Base <Item>
{
public:
bool DoIt( /*...*/ ) const
{
// ...
// return true on successful work.
}
};

//---------------------------
// Expression object (OR)
//---------------------------
template <typename T1, typename T2>
class OrExpression : Base < OrExpression<T1, T2> >
{
public:
OrExpresion(T1 const& l, T2 const& r)
: m_l(l), m_r(r)
{ }

bool DoIt( /*...*/ ) const
{
return m_l.DoIt( /*...*/ ) | m_r.DoIt( /*...*/ );
}
private:
T1 m_l;
T2 m_r;
};

template <typename T1, typename T2>
inline OrExpression<T1, T2>
operator|(Base<T1> const& l, Base<T2> const& r)
{
return OrExpression<T1, T2>(l.Get(), r.Get());
}

// etc...

With respective operator overloads, it allows me to write
expressions of Item-like objects:

template <typename T>
void DoSomething(Base<T> const& expr);

Item a, b, c;
DoSomething(a & b | c);

However, I can only use those expressions to generate temporary
objects. If I wanted to assign such an expression to a variable,
I'd have to explicitly specify its type...

My question: Is there a technique on such use of assignable
expression templates?

E.g.
Item a, b, c;
MyExpression e;
e = a & b | c;

Thank you in advance for any hints...
Regards,
bartek

Jul 22 '05 #1
8 1427
bartek wrote:
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 are wrapped around following the
'curious base' pattern.
....
However, I can only use those expressions to generate temporary
objects. If I wanted to assign such an expression to a variable,
I'd have to explicitly specify its type...

My question: Is there a technique on such use of assignable
expression templates?

E.g.
Item a, b, c;
MyExpression e;
e = a & b | c;

Thank you in advance for any hints...


You need to have a base expression class and you need to create non
temporary objects with some kind of object lifetime management
(reference counting).

Having a different base type for every type of node will be a problem.

i.e. if you do this:

Item & e = DeepCopy( a & b | c );

You need to know when to delete what e is referencing.

So you'll need some kind of smart pointer.

pointer<Item> e = DeepCopy( a & b | c );

I can provide more details later but suffice to say, you need to use
dynamic objects. The "operator &(...)" call needs to return a reference
to a dynamically allocated object i.e.

Item & operator&( Item & rhs )
{
return * new AndThing( * this, rhs );
}
G


Jul 22 '05 #2
Gianni Mariani <gi*******@mariani.ws> wrote in news:c1dh2d
$k**@dispatch.concentric.net:
bartek wrote:
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 are wrapped around following the
'curious base' pattern.

...

However, I can only use those expressions to generate temporary
objects. If I wanted to assign such an expression to a variable,
I'd have to explicitly specify its type...

My question: Is there a technique on such use of assignable
expression templates?

E.g.
Item a, b, c;
MyExpression e;
e = a & b | c;

Thank you in advance for any hints...


You need to have a base expression class and you need to create non
temporary objects with some kind of object lifetime management
(reference counting).

Having a different base type for every type of node will be a problem.

i.e. if you do this:

Item & e = DeepCopy( a & b | c );

You need to know when to delete what e is referencing.

So you'll need some kind of smart pointer.

pointer<Item> e = DeepCopy( a & b | c );

I can provide more details later but suffice to say, you need to use
dynamic objects. The "operator &(...)" call needs to return a reference
to a dynamically allocated object i.e.

Item & operator&( Item & rhs )
{
return * new AndThing( * this, rhs );
}


I thought of using an expression base class with the virtual DoIt method,
and overloaded assignment operator to make up a polymorphic type. Something
like the following (just guessing):

class PolyExpressionBase
{
public:
virtual bool DoIt( /*...*/ ) const = 0;
};

template <typename T>
class PolyExpression : public PolyExpressionBase
{
PolyExpression(T& const expr)
: m_expr(expr)
{ }

bool DoIt( /*...*/ ) const
{
return m_expr.DoIt( /*...*/ );
}

private:
T m_expr;
};

class Expression
{
public:
/* ...ctors, dtor, etc... */

template <typename T>
Expression& operator=(T const& expr)
{
/* ...cruft... */
m_expr = new PolyExpression<T>(expr);
return *this;
}

/* ...cruft... */

private:
PolyExpressionBase* m_expr;
};

It seems it would sort of encapsulate the static structure of an expression
in a dynamic object. However I really would like to keep things as much
static as possible.

Thanks a lot for the hint.
Regards,
bartek
Jul 22 '05 #3
There are many who believe that the auto keyword should be overloaded
to ease your pain (google author:David Abrahams auto proposal). You
might find it worthwhile to look at spirit to see how they do it, too.
Static binding is possible if you're willing to tolerate ugly code.

bartek <ba*****@qwertyuiop.o2.pl> wrote in message news:<Xn**********************************@153.19. 251.200>...
...
My question: Is there a technique on such use of assignable
expression templates?

E.g.
Item a, b, c;
MyExpression e;
e = a & b | c;

Thank you in advance for any hints...
.... You need to have a base expression class and you need to create non
temporary objects with some kind of object lifetime management
(reference counting).


Thanks a lot for the hint.
Regards,
bartek

Jul 22 '05 #4
ha*********@yahoo.com (anon luker) wrote in
news:53*************************@posting.google.co m:
There are many who believe that the auto keyword should be overloaded
to ease your pain (google author:David Abrahams auto proposal). You
might find it worthwhile to look at spirit to see how they do it, too.
Static binding is possible if you're willing to tolerate ugly code.


I am aware of the 'auto proposal'. I wouldn't mind it, of course. It
doesn't help me at the moment, though.

I'm looking inside of Spirit at the moment, but it's ... kind of ...
complex... MY goodness!!! Am I going to figure it out someday AT ALL!?
Doh!

Cheers,
bartek
Jul 22 '05 #5
bartek wrote:
Gianni Mariani <gi*******@mariani.ws> wrote in news:c1dh2d
$k**@dispatch.concentric.net: ....
It seems it would sort of encapsulate the static structure of an expression
in a dynamic object. However I really would like to keep things as much
static as possible.


This is more or less the way I did it a while back and it works just
fine (as long as you reference count it or manage it's life-time some
other way - I used reference counting with a special case where objects
were created with a reference count of 0 - this simplified the code
somewhat).

There really is no other way to make an expression like the one you
showed do this.

I also had a number of other methods to help with optimizations etc.
The expression was a template that was based on a "value type" and the
value type needed to support all the methods for managing themselves.

As for making things as static as possible, yep, it would be nice but it
really does not matter since you never really need to worry about the
performance (at least I didn't).

Jul 22 '05 #6
well... what you're trying to do is hard in nature. Something worth
keeping in mind, though, is that if the compiler can instantiate your
function template for assignment then the resulting type is known at
compile time. Determination of that type is independent of the
semantics of the actual assignment - whether you use a deep copy or a
shallow one, whether you ref count or not, all of those choices are
independent of your ability to properly instantiate an object to hold
the result. The alternative is to kludge and make an inhomogeneous
bag style container - not recommended.

bartek <ba*****@qwertyuiop.o2.pl> wrote in message news:<Xn*********************************@153.19.2 51.200>...
ha*********@yahoo.com (anon luker) wrote in
news:53*************************@posting.google.co m:
There are many who believe that the auto keyword should be overloaded
to ease your pain (google author:David Abrahams auto proposal). You
might find it worthwhile to look at spirit to see how they do it, too.
Static binding is possible if you're willing to tolerate ugly code.


I am aware of the 'auto proposal'. I wouldn't mind it, of course. It
doesn't help me at the moment, though.

I'm looking inside of Spirit at the moment, but it's ... kind of ...
complex... MY goodness!!! Am I going to figure it out someday AT ALL!?
Doh!

Cheers,
bartek

Jul 22 '05 #7
ha*********@yahoo.com (anon luker) wrote in
news:53**************************@posting.google.c om:
well... what you're trying to do is hard in nature. Something worth
keeping in mind, though, is that if the compiler can instantiate your
function template for assignment then the resulting type is known at
compile time. Determination of that type is independent of the
semantics of the actual assignment - whether you use a deep copy or a
shallow one, whether you ref count or not, all of those choices are
independent of your ability to properly instantiate an object to hold
the result. The alternative is to kludge and make an inhomogeneous
bag style container - not recommended.


So there's actually no point in doing such a thing. The only benefit would
be making a nice 'sugared up' expression interface using overloaded
operators, but at the expense of having contrived complex dynamic code hell
underneath. I begin wondering, is it really worth the effort?

Cheers,
bartek
Jul 22 '05 #8
bartek <ba*****@qwertyuiop.o2.pl> wrote:
[...]
My question: Is there a technique on such use of assignable
expression templates?

E.g.
Item a, b, c;
MyExpression e;
e = a & b | c;
The only thin I can think of is that, if
you can delegate the task to a function,
you can have the compiler figure it out
for you:

template< typename T >
void f(const T& expr)
{
T e = expr;
// use e
}

void g()
{
// ...
f( a & b | c );
// ...
}

However, 'f()' will be called at run-
time. This is not a compile-time feature.
Thank you in advance for any hints...
Regards,
bartek

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #9

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...
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; ...
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...
3
by: massysett | last post by:
I'm puzzled about part of the standard. 23.1 states that items stored in a container must be assignable. Therefore, the items in a map--that is, std::pair<const Key, valuemust be assignable....
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. ...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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...

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.