Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why Koenig's lookup does not work for templates?

4 views
Skip to first unread message

Peng Yu

unread,
Oct 22, 2008, 12:32:22 AM10/22/08
to
Hi,

I'm wondering why Koenig's lookup does not work for templates. Please
see below for the example and the reason why I raise this question (in
the comments).

Thanks,
Peng

#include <iostream>

namespace A1 {

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 A2 {

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>
#if 0
Y<typeof(T1() * T2())>
// In principle, this should work.
// But it actually does not work due a bug in GCC, the compiler
that I use.
#else
Y<typename A1::multiply_traits<T1, T2>::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<typename multiply_traits<T1, T2>::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<T1> &y, const T2 &t) {
return Y<T1>(y.the_t() * t);
}
}

int main () {
A1::X<int> x1(-2);
A2::X<int> x2(2);
B::Y<A1::X<int> > y1(x1);
B::Y<A2::X<int> > 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
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

wasti...@gmx.net

unread,
Oct 24, 2008, 6:32:03 PM10/24/08
to
On Oct 22, 6:32 am, Peng Yu <PengYu...@gmail.com> wrote:
> Hi,
>
> I'm wondering why Koenig's lookup does not work for templates. Please
> see below for the example and the reason why I raise this question (in
> the comments).

If you allow Koenig lookup for finding class templates, you have to do
overload resolution on class templates. That was probably considered
too complicated to specify - especially since Koenig lookup itself was
a pretty late addition to C++. (I think. My D&E edition from 1994
doesn't mention it, at least.)

Peng Yu

unread,
Oct 25, 2008, 5:18:09 PM10/25/08
to
On Oct 24, 5:32 pm, wasti.r...@gmx.net wrote:
> On Oct 22, 6:32 am, Peng Yu <PengYu...@gmail.com> wrote:
>
> > Hi,
>
> > I'm wondering why Koenig's lookup does not work for templates. Please
> > see below for the example and the reason why I raise this question (in
> > the comments).
>
> If you allow Koenig lookup for finding class templates, you have to do
> overload resolution on class templates. That was probably considered
> too complicated to specify - especially since Koenig lookup itself was
> a pretty late addition to C++. (I think. My D&E edition from 1994
> doesn't mention it, at least.)


Do you mean that Koenig lookup is worthwhile to be added for finding
class templates in future standard? It is not there because it needs
more time to standardize it and implemented? Do you mean that it is
possible to add Koenig lookup for finding class templates in future
standard?

BTW, what is your D&E edition?

Thanks,
Peng

David Abrahams

unread,
Oct 26, 2008, 11:18:07 AM10/26/08
to

on Wed Oct 22 2008, Peng Yu <PengYu.UT-AT-gmail.com> wrote:

> I'm wondering why Koenig's lookup does not work for templates.

This doesn't explain why, but...

Argument-dependent (Koenig) lookup causes enough problems as it is
(http://www.boostpro.com/writing/n1691.html). Many of us are looking
for ways to limit its impact, so I really hope its effect isn't extended
at all.

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

0 new messages