Re: Is it possible to limit the template argument to a few class types?
fabio_cannizzo@yahoo.com writes:
I am trying to override some basic arithmetic operators, e.g.
class A {};
class B {};
class C {};
template <class A1, class A2>
MyClass<A1,A2> operator +( const A1&, const A2& ) {}
Here, both template arguments A1 and A2 can be either of type A, B or
C, but nothing else.
As soon as I write the code above, it clashes with other similar
template functions declared, for istance, in STL iterators, and the
compiler get confused at which one it should use.
Obviously, I can solve the problem if instead of using templates, I
use overload for all the posible combinations of A, B, and C, or if I
define all possible the overload as members of the class A, B and C.
But I was wondeing if there is any magic I can do, may be inheriting
A, B and C from some common class, to keep a unique definition, as
above.
You are not very specific about the nature of your problems.
But in general, it's a good idea to avoid declaring names in the
global namespace. Do you problems persist if you wrap classes A, B and
C and the operator+ in a namespace?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]