AW: G++ Bug in finding template instanciation
Hyman Rosen
hymie@prolifics.com
Wed May 10 09:04:00 GMT 2000
Actually, I need to amend my last statement about Martin v. Loewis
being wrong. My analysis is right, but so is his. Here's a capsule
description of the problem:
template <class T>
struct W
{
struct I { };
};
template <class T>
int operator-(typename W<T>::I, typename W<T>::I)
{
return 5;
}
template <class X>
int diff(X a, X b)
{
return a - b;
}
int main()
{
return diff(W<char>::I(), W<char>::I());
}
Those Ts in your operator- are in a non-deduced context. One reason is
that such deduction is subject to ambiguity. Suppose I write this:
template<> struct W<int> { typedef W<char>::I I; };
Now the call to operator- could potentially deduce that T is char or int!
So, are you screwed? Not at all! This where the Barton & Nackman friend
trick comes into play, as described in 14.5.3/5. Define your operator-
as a friend *inside* your structure template, like this:
template <class T>
struct W
{
struct I { };
friend int operator-(I, I)
{
return 5;
}
};
Now, because of 14.5.3/5, an operator- of the correct signature is defined
whenever a W is instantiated. No type deduction needs to happen, and the
correct operator- is found, subject to the unwanted name hiding problem
that I mentioned in the previous message.
More information about the Gcc-bugs
mailing list