views:

590

answers:

4

I am working on making an expression class:

template<typename T, typename U>
class expression
{
public:
    expression(T vala, U valb, oper o){val1 = vala; val2 = valb; op = o;}
    operator bool{return(val1 op val2);}
private:
    T val1;
    U val2;
    oper op;
};

as you can see, this is somewhat pseudocode, because I need an operator class. My original thought was to create an array of all possible operators, and then convert it through a string, but that wouldn't work because of the sheer number of operators, and how to convert it to a string except through a two dimensional array, where n[0][0] has the first operator, and n[0][1] has that operators string.

Does anybody have any suggestions to adding an operator value to my expression class?

+1  A: 

I'm not entirely sure what you're asking, but if you are trying to overload an arbitrary string as an operator, you can't. There is a finite set of operators that you can overload in c++

see here: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

What you should do is overload operator() in oper to create a function object and return op(val1, val2) instead.

Charles Ma
A: 

Maybe a function pointer. Instead of ...

operator bool{return(val1 op val2);}

... code it as ...

operator bool{return op(val1, val2);}

... in which case op can be a pointer to a (any) function which takes two parameters and which returns bool.

template<typename T, typename U>
class expression
{
public:
    //define pointer-to-function type
    typedef bool *oper(const T& val1, const U& val2);
    ... etc ...
ChrisW
Why did you use typedef?
Keand64
I used typedef to define what the 'oper' type is, which the OP passes asa paameter to the expression constructor: I'm saying that it's a pointer to a function, which takes two parameters and returns bool.
ChrisW
+1  A: 

Similar methods are used in c++ expression templates techniqueue.

You create expression as a class with a method such as apply or evaluate. This method takes the parameters and applies the expression.

Have a look what expression templates are using. http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm http://ubiety.uwaterloo.ca/~tveldhui/papers/Expression-Templates/exprtmpl.html

As an example in your case:

struct isEqual
{
    template <typename T, typename U>
    bool operator()(T a, U b)
    {
        return a == b;
    }
};

template <typename T, typename OP>
struct expression
{
    T& a;
    T& b;
    OP& op;

    expression(T a, T b, OP op) : a(a), b(b), op(op) {}

    void eval() { op(a,b); }
};


int main()
{
    expression<int, isEqual> exp(1,2,isEqual());
    exp.eval();
}
stefanB
A: 

You can use the functional standard library and take your argument as a:

std::tr1::function<bool (T,U)>

ie:

#include <functional>

template<typename T, typename U>
class expression
{
public:
   expression(T vala, U valb, oper o) : val1(vala), val2(valb), op(o)
   { }
   operator bool{return op(val1, val2);}
private:
   T val1;
   U val2;
   std::tr1::function<bool (T,U)> op;
};

Then, to create a expression:

#include <functional>

expression<int, int> foo(4,3, std::tr1::bind(greater()));

Here's a tutorial

Todd Gardner