test_templates.cxx 702 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "decaf/expr.hxx"

class Foo {
private:
    template<BinOp OP, class L, class R, class T> friend class BinExpr;
    template<UnOp OP, class R, class T> friend class UnExpr;
    template<class T> friend struct Reify;
    Foo(const NOINIT&) {}
public:
    int x;
    Foo(int x) : x(x) {}
};

namespace decaf { namespace internal {
DECLARE_BINOP(ADD,Foo,Foo,Foo,out.x = l.x+r.x)
DECLARE_BINOP(SUB,Foo,Foo,Foo,out.x = l.x-r.x)
DECLARE_BINOP(MUL,Foo,Foo,Foo,out.x = l.x*r.x)
DECLARE_BINOP(EQ,Foo,Foo,bool,out = l.x==r.x)
DECLARE_PARTIAL_UNOP(INV,Foo,Foo,out.x = 1/r.x; return (r.x!=0))
}}

Foo frobble() {
    Foo a(1);
    a = a+a+a;
    a = a*a;
    a = a/a;
    (void)(a==(a+a));
    return a;
}