This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
AW: G++ Bug in finding template instanciation
- To: kanze at gabi-soft dot de
- Subject: AW: G++ Bug in finding template instanciation
- From: Hyman Rosen <hymie at prolifics dot com>
- Date: Wed, 10 May 2000 11:32:10 -0400
- Cc: gcc-bugs at gcc dot gnu dot org
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-.