This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: 2.96: Templ. friend vs. member operators


On Mon, Jan 10, 2000 at 09:16:09PM +0100, Kurt Garloff wrote:
> gcc-2.96 does not accept the declaration of both a templated friend operator
>  friend MyClass<T> operator+ <> (const MyClass<T>&, const MyClass<T>&);
> and a member operator
>  MyClass<T> operator+ (const MyClass<T>&) const;
> [...]
> garloff@etpvis:~/gum/testsuite/compiler > g++ -O2 templfriend.cpp -DFRIENDOPERATOR -o templfriend
>  templfriend.cpp: In instantiation of `MyClass<int>':
>  templfriend.cpp:64:   instantiated from here
>  templfriend.cpp:44: invalid use of undefined type `class MyClass<int>'
>  templfriend.cpp:36: forward declaration of `class MyClass<int>'
>  templfriend.cpp:44: confused by earlier errors, bailing out

I did forget to actually attach the program, when sending this to you. 
Sorry! Here it comes.

-- 
Kurt Garloff                 <kurt@garloff.de>		    [Eindhoven, NL]
Physics: Plasma simulations  <k.garloff@phys.tue.nl>	 [TU Eindhoven, NL]
Linux: SCSI, Security        <garloff@suse.de>	      [SuSE Nuernberg, FRG]
(See mail header or public servers for PGP2[RSA] and GPG[DSA] public keys.)
/* templfriend.cpp */
/*
 * Code to show gcc-2.96 does NOT allow both a member operator+
 * and a templated friend operator+ for the same args. (What happens
 * if the args are different, BTW?).
 * Don't know, what the C++ draft standard says on this. KG. 1/2000.
 * Testcase by Jan van Dijk <jan@etpmod.phys.tue.nl>, 1/2000.
 * Copyright: BSD.
 */

#include <iostream>

// forward declaration
template <typename T> class MyClass;

template <typename T>
ostream& operator<< ( ostream& os, const MyClass<T>& mc)
{
	os << mc.private_data;
	return os;
}

#ifdef FRIENDOPERATOR
// template friend of MyClass
template <typename T>
MyClass<T> operator+ (const MyClass<T>& m1, const MyClass<T>& m2)
{
	cout << "Using friend operator+" << endl;
	return MyClass<T> (m1.private_data + m2.private_data);
}
#endif

// minimal test class
template <typename T>
class MyClass
{
 public:
	MyClass (const T& val) { private_data = val; }
#ifndef NOMEMBEROPERATOR	
	MyClass<T> operator+ (const MyClass<T>& another) const;
#endif
	friend ostream& operator<< <> (ostream& os, const MyClass<T>& mc);
#ifdef FRIENDOPERATOR
	friend MyClass<T> operator+ <> (const MyClass<T>&, const MyClass<T>&);
#endif
 private:
	T private_data;
};

#ifndef NOMEMBEROPERATOR	
template <typename T>
MyClass<T> MyClass<T>::operator+ (const MyClass<T>& another) const
{
	cout << "Using member operator+" << endl;
	MyClass<T> mc(*this);
	mc.private_data += another.private_data;
	return mc;
}
#endif

int main( void)
{
	// instantiation with int
	MyClass<int> mc1(1);
	MyClass<int> mc2(2);
	cout << "mc1 + mc2 = " << mc1 + mc2 << endl;

	return 0;
}

PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]