Re: template parameter's namespace exposed?
Matjaz Depolli wrote:
I've got a problem that I will try to demonstrate with the
following code:
namespace N {
class A {
};
template<class T1, class T2>
T1 operator+ (T1 op1, T2 op2) {return op1;}
}
template<class T>
class B {
};
int main() {
N::A a1, a2;
a1 + a2; // works as it should
B<N::A> b1, b2;
b1 + b2; // oops, this one compiles too!
Right. Because the namespace N is associated with the types of
b1 and b2, it is pulled into the search as well.
B<int> b3, b4;
b3 + b4; // no match for operator +, just the way it should be
}
The unwanted behaviour here is, that templated operator inside
the namespace makes itself visible to the templated classes
outside that namespace, instantiated with a member of the same
namespace as their template parameter. It is not like B
extends N::A, in which case this would be ok.
B, as such, has nothing to do with it. The type of b1 and b2 is
B<N::A>, however, so the namespaces containing both B and A are
associated with them.
Just for the background of the problem - I've written
expression templates for my algebraic vector class and found
myself bumping my head into this wall. Now I can't even make
my vector part of std containers because operator+ messes too
much with their inner workings.
I find this problem quite surprising and I'm wandering whether
this is a gcc bug (I'm using gcc version 3.4.4 -mingw special)
or is this the way the standard says it should be. If the
latter is the case than I have just found out the hard way
that fully templated operators are too great of a beast to let
loose - even in a well hidden namespaces :)
You've just found out the hard way that you should never provide
a template for operator+ that depends strictly on type.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]