#include #include namespace A1 { template class X { public: X() { } X(T t) : _t(t) { } const T &the_t() const { return _t; } private: T _t; }; template struct multiply_traits; template struct multiply_traits, T2> { typedef X result_type; }; template typename multiply_traits, T2>::result_type operator*(const X &x, const T2 &t) { return X(x.the_t() * t); } } namespace A2 { template class X { public: X() { } X(T t) : _t(t) { } const T &the_t() const { return _t; } private: T _t; }; template struct multiply_traits; template struct multiply_traits, T2> { typedef X result_type; }; template typename multiply_traits, T2>::result_type operator*(const X &x, const T2 &t) { return X(x.the_t() * t); } } namespace B { template class Y { public: Y(T t) : _t(t) { } const T &the_t() const { return _t; } private: T _t; }; template #if 1 Y // In principle, this should work. // But it is actually not due a bug in GCC, the compiler that I use. #else Y::result_type> // This only makes the line marked with L1 work, // but the line marked with L2 does not work. // I would like to use the following command and hope // Koenig lookup would work for tempalte, but it is not. // Y::result_type> // // I'm wondering what the reason is not to have Koenig lookup // for templates. Is it because this problem hasn't been considered before? // Or it is because there is some drawback if Koenig lookup is allowed for templates. #endif operator*(const Y &y, const T2 &t) { return Y(y.the_t() * t); } } int main () { A1::X x1(-2); A2::X x2(2); B::Y > y1(x1); B::Y > y2(x2); std::cout << (x1 * 3).the_t() << std::endl; std::cout << (x2 * 3).the_t() << std::endl; std::cout << (y1 * 5).the_t().the_t() << std::endl; //L1 std::cout << (y2 * 5).the_t().the_t() << std::endl; //L2 }