P
Peng Yu
Hi,
I'm wondering if Koenig lookup can be applied somehow to derive which
template to use based on the template arguments.
The following code shows an example where multiply_traits's namespace
A has to be specified in the definition of Y. This makes Y unusable
for any multiply_traits defined in other namespaces, which
corresponding T1 and T2 defined in those namespaces. See also comments
in the code.
Thanks,
Peng
#include <iostream>
namespace A {
template <typename T>
class X {
public:
X() { }
X(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};
template <typename T1, typename T2>
struct multiply_traits;
template <typename T1, typename T2>
struct multiply_traits<X<T1>, T2> {
typedef X<T1> result_type;
};
template <typename T1, typename T2>
typename multiply_traits<X<T1>, T2>::result_type operator*(const
X<T1> &x, const T2 &t) {
return X<T1>(x.the_t() * t);
}
}
namespace B {
template <typename T>
class Y {
public:
Y(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};
template <typename T1, typename T2>
Y<typename A::multiply_traits<T1, T2>::result_type>
#if 0
I want to specify the template argument without explicitly say the
namespace A.
If this could be possible, Y can be used for types that are defined
in any namespace as long as a corresponding multiply_traits are
defined in such namespace.
But I just don't find such a way.
Is there anybody know if there is any walkaround?
#end if
operator*(const Y<T1> &y, const T2 &t) {
return Y<T1>(y.the_t() * t);
}
}
int main () {
A::X<int> x(2);
B::Y<A::X<int> > y(x);
std::cout << (x * 3).the_t() << std::endl;
std::cout << (y * 5).the_t().the_t() << std::endl;
}
I'm wondering if Koenig lookup can be applied somehow to derive which
template to use based on the template arguments.
The following code shows an example where multiply_traits's namespace
A has to be specified in the definition of Y. This makes Y unusable
for any multiply_traits defined in other namespaces, which
corresponding T1 and T2 defined in those namespaces. See also comments
in the code.
Thanks,
Peng
#include <iostream>
namespace A {
template <typename T>
class X {
public:
X() { }
X(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};
template <typename T1, typename T2>
struct multiply_traits;
template <typename T1, typename T2>
struct multiply_traits<X<T1>, T2> {
typedef X<T1> result_type;
};
template <typename T1, typename T2>
typename multiply_traits<X<T1>, T2>::result_type operator*(const
X<T1> &x, const T2 &t) {
return X<T1>(x.the_t() * t);
}
}
namespace B {
template <typename T>
class Y {
public:
Y(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};
template <typename T1, typename T2>
Y<typename A::multiply_traits<T1, T2>::result_type>
#if 0
I want to specify the template argument without explicitly say the
namespace A.
If this could be possible, Y can be used for types that are defined
in any namespace as long as a corresponding multiply_traits are
defined in such namespace.
But I just don't find such a way.
Is there anybody know if there is any walkaround?
#end if
operator*(const Y<T1> &y, const T2 &t) {
return Y<T1>(y.the_t() * t);
}
}
int main () {
A::X<int> x(2);
B::Y<A::X<int> > y(x);
std::cout << (x * 3).the_t() << std::endl;
std::cout << (y * 5).the_t().the_t() << std::endl;
}