This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
A bug in namespaced template overloaded operator << and >>
- To: gcc-bugs at gcc dot gnu dot org
- Subject: A bug in namespaced template overloaded operator << and >>
- From: Topi Maenpaa <topiolli at ees2 dot oulu dot fi>
- Date: Wed, 09 Aug 2000 13:51:46 +0300
- Organization: University of Oulu, Department of Electrical Engineering, Finland
Hi,
I have had some weird problems in template overloaded functions that are
enclosed in a namespace. I have produced a simplified version of the
structure I have been trying to compile. For this code, g++ says:
Test5.cc: In function `int main()':
Test5.cc:68: ambiguous overload for `_IO_ostream_withassign & <<
foo::rgbcolorimage &'
Test5.cc:36: candidates are: class ostream & foo::operator
<<<foo::rgbcolor>(ostream &, foo::matrix<foo::rgbcolor> &)
Test5.cc:47: class ostream & foo::operator
<<<foo::rgbcolor>(ostream &, foo::image<foo::rgbcolor> &)
Test5.cc:55: class ostream & foo::operator
<<<foo::rgbcolor>(ostream &, foo::image<foo::rgbcolor> &)
After a few days of wasted time I got it work by removing the "namespace
foo" and "using namespace foo". Since this was not an acceptable
solution to me, I figured out another way: you can do it by moving the
template definitions for operator << (ostream, color<T,i>) and operator
<< (ostream, image<T>) *before* the definitions of class rgbcolor and
rgbcolorimage, respectively. (Guess how long it took? Enough to drive me
crazy...)
---------8<---- Test5.cc ----8<---------
#include <iostream>
namespace foo
{
template <class T, int i>
class color
{
public:
template <class U, int j> friend ostream& operator << (ostream& sout,
color<U,j>& obj);
};
class rgbcolor : public color<int,3>
{
};
template <class T, int i> ostream& operator << (ostream& sout,
color<T,i>& obj)
{
sout << i;
return sout;
}
template <class T>
class matrix
{
public:
template <class U> friend ostream& operator << (ostream& sout,
matrix<U>& obj);
protected:
T element;
};
template <class T> ostream& operator << (ostream& sout, matrix<T>& obj)
{
sout << obj.element;
return sout;
}
template <class T>
class image : public matrix<T>
{
public:
template <class U> friend ostream& operator << (ostream& sout,
image<U>& obj);
};
class rgbcolorimage : public image<rgbcolor>
{
};
template <class T> ostream& operator << (ostream& sout, image<T>& obj)
{
sout << obj.element;
return sout;
}
}
using namespace foo;
int main(void)
{
rgbcolorimage img;
cout << img;
}
--
-Topi-
<< http://ee.oulu.fi/~topiolli >>