Re: How can I use unqualified names? (Possibly hard or impossible?)
On Jul 19, 11:52 am, Jonathan Lee <cho...@shaw.ca> wrote:
On Jul 19, 11:47 am, Jonathan Lee <cho...@shaw.ca> wrote:
Not sure how valuable my advice is, but here are my thoughts:
So the following works, but it's kinda ugly. OK, it's really ugly. I
had to introduce a partial specialization of Options<type1, type2> to
get it to compile. I chose void* as type2; I don't really know how to
get around this. This means that I had to define a operator<<(void*)
on the test class Bar. And, basically there are void*'s scattered all
over. But the usage is as I described above.
I also defined trivial classes to wrap double (called xbase, zbase)
and int (called ybase). Maybe a typedef is sufficient?
// code -----------------------------------------
#include <iostream>
class xbase {
public:
double x;
xbase(double y) : x(y) {}
};
class ybase {
public:
int y;
ybase(int x) : y(x) {}
};
class zbase {
public:
double z;
zbase(double y) : z(y) {}
};
template<typename T, typename U>
class Options {
public:
T val;
U other;
Options(T t, U u) : val(t), other(u) { }
};
template<typename T>
class Options<T, void*> {
public:
T val;
void* other;
Options(T x) : val(x) {}
};
template<typename T, typename U, typename V>
Options<Options<T, U>, V> operator&(Options<T, U> t, Options<V, void*>
u) {
return Options<Options<T, U>, V>(t,u.val); }
class Bar {
xbase x1;
zbase z1;
ybase y1;
public:
Bar() : x1(0.0), z1(0.0), y1(0) { }
void print() {
std::cout << x1.x << std::endl;
std::cout << y1.y << std::endl;
std::cout << z1.z << std::endl;
}
Bar& operator<<(zbase zz) {
z1 = zz;
return (*this);
}
Bar& operator<<(xbase xx) {
x1 = xx;
return (*this);
}
Bar& operator<<(ybase yy) {
y1 = yy;
return (*this);
}
Bar& operator<<(void*) {
return (*this);
}
template<typename T, typename U>
Bar& operator<<(Options<T, U> otu) {
operator<<(otu.val);
operator<<(otu.other);
return (*this);
}
};
typedef Options<xbase, void*> xtype;
typedef Options<zbase, void*> ztype;
typedef Options<ybase, void*> ytype;
int main() {
Bar foo;
foo << (xtype(1.0) & ztype(2.0) & ytype(18));
foo.print();
return 0;
}