B
bartek
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
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