C++ bug

Mark van Doesburg m.j.s.vandoesburg@student.utwente.nl
Wed May 13 05:55:00 GMT 1998


I've reported this bug before with egcs-1.0.2, it also crashes gcc-2.8.1
the second problem _might_ be a bug, but I'm not sure.

OS = Linux-2.0.33
libc = libc.so.5.4.33 (compiled with gcc-2.7.2.2)
CPU = Intel Pentium && AMD-K5

---------------- internal compiler error  --------
This happens with #if 1

tm.cpp: In method `class matrix<int,3> matrix<int,3>::operator *<int>(const class matrix<int,3> &)':
tm.cpp:116: Internal compiler error.
tm.cpp:116: Please submit a full bug report to `egcs-bugs@cygnus.com'.

---------------- my error ? ----------------
This happens with #if 0

tm.cpp: In method `class matrix<int,3> matrix<int,3>::operator *<int>(const class matrix<int,3> &)':
tm.cpp:118: ambiguous overload for `coord<int,3> & * coord<int,3>'
tm.cpp:44: candidates are: coord<int,3>::operator *<int>(const coord<int,3> &)
tm.cpp:54:                 coord<int,3>::operator *<coord<int,3>>(coord<int,3>)
tm.cpp:63:                 operator *<coord<int,3>, 3, int>(coord<int,3>, coord<int,3>)

In my opinion the function resolved should be the member on line 44
since it is the best match. And it is the resolved function if the first
operand is a coord (and not a coord&) Also the draft of 2 Dec 1996
states in 13.3.1 par 4 -

	For non-static member functions, the type of the implicit object
	parameter is "reference to cv X" ....

Which is what we have here.  I do think the compiler is wrong in this
case, but since I'm not an C++ expert (The last time I wrote a C++
program was in 1994) I could be wrong.


Thanks for any bug-fixes/comments, 
	Mark.

---------------- tm.cpp --------------------------------
template<class T,int S> 
class coord {
	T c[S];
public: 
	coord() {};
	template<class TT> coord(coord<TT,S> const &o);

	T &operator[](int i) { return c[i]; }
	T get(int i) const { return c[i]; } 

	coord<T,S> operator+(coord<T,S> const &o);
	coord<T,S> operator-(coord<T,S> const &o); 
	template<class TT> T operator*(coord<TT,S> const &o);
	template<class TT> coord<T,S> operator*(TT o);
};

template<class T,int S> template<class TT>
inline coord<T,S>::coord(coord<TT,S> const &o)
{
	for(int i=0;i<S;i++) 
		c[i]=o.get(i); 
}

template<class T,int S>
inline coord<T,S> coord<T,S>::operator+(coord<T,S> const &o)
{
	coord<T,S> t;
	for(int i=0;i<S;i++) 
		t.c[i]=c[i]+o.c[i]; 
	return t;
}

template<class T,int S>
inline coord<T,S> coord<T,S>::operator-(coord<T,S> const &o)
{
	coord<T,S> t;
	for(int i=0;i<S;i++) 
		t.c[i]=c[i]-o.c[i]; 
	return t;
}

template<class T,int S> template<class TT>
inline T coord<T,S>::operator*(coord<TT,S> const &o)
{
	T t;
	t=c[0]*o.get(0);
	for(int i=1;i<S;i++) 
		t+=c[i]*o.get(i); 
	return t;
} 

template<class T,int S> template<class TT>
inline coord<T,S> coord<T,S>::operator*(TT o)
{
	coord<T,S> t;
	for(int i=0;i<S;i++) 
		t[i]=c[i]*o; 
	return t;
} 

template<class T,int S,class TT>
inline coord<T,S> operator*(T a,coord<TT,S> b)
{ 
	return (coord<T,S>)b*a;
} 

template<class T,int S>
class matrix {
	coord<T,S> r[S];
public:
	matrix() {}
	template<class TT> matrix(matrix<TT,S> const &); 

	coord<T,S> get(int i) const { return r[i]; }
	coord<T,S> &operator[](int i) { return r[i]; } 
	coord<T,S> col(int) const;
	coord<T,S> row(int i) const { return r[i]; }


	template<class TT> coord<T,S> operator*(coord<TT,S> const &);
	template<class TT> matrix<T,S> operator*(matrix<TT,S> const &);
};

template<class T,int S> template<class TT>
inline matrix<T,S>::matrix(matrix<TT,S> const &o)
{
	for(int i=0;i<S;i++)
		r[i]=o.get(i);
} 

template<class T,int S>
inline coord<T,S> matrix<T,S>::col(int i) const
{
	coord<T,S> t;
	for(int j=0;j<S;j++)
		t[j]=r[j].get(i);
	return t;
}

template<class T,int S> template<class TT>
inline coord<T,S> matrix<T,S>::operator*(coord<TT,S> const &o)
{
	coord<T,S> t;
	for(int i=0;i<S;i++)
		t[i]=r[i]*o;
	return t;
}

template<class T,int S> template<class TT>
inline matrix<T,S> matrix<T,S>::operator*(matrix<TT,S> const &o)
{
	matrix<T,S> t;
	for(int i=0;i<S;i++)
		for(int j=0;j<S;j++) {
#if 1
			t[i][j]=row(i)*o.col(j);
#else
			t[i][j]=r[i]*o.col(j); 
#endif
		}
	return t;
}

main()
{
	matrix<int,3> m1;
	matrix<int,3> m2; 
	matrix<int,3> m3; 
	coord<int,3> v1;
	coord<int,3> v2;
	v2=m1*v1;
	double t=v1*v2;
	m3=m1*m2;
}



More information about the Gcc-bugs mailing list