AW: G++ Bug in finding template instanciation

Hyman Rosen
Wed May 10 08:32:00 GMT 2000

I saw the bug you reported to gcc-bugs regarding the failure of the
compiler to find the operator- for your Wrapper<string>::Iter
classes. The bug is in your code, but Martin v. Loewis' interpretation
of what is wrong is incorrect. You are running into a situation that
is described by Herb Sutter in Exceptional C++ on page 136. It is an
example of what he calls 'unwanted name hiding'.

Your problem is that you have declared your operator- for
Wrapper<T>::Iter in the global namespace. Unfortunately for you, the
standard algorithms are in namespace std, and name lookup proceeds
from current scope outwards, and stops as soon as a matching name is
found. That means that if there is an operator- defined in namespace
std, for *any type at all*, your operator- will never be seen.

The solution, as Herb Sutter explains, is to put your classes and
functions that pertain to them, in a namespace. Now, Koenig lookup
becomes involved.  When the compiler searches for an operator-, it
will also look in the namespaces of the operands, and then it will
find your operator-.

More information about the Gcc-bugs mailing list