public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ 2.95.3 template function bug
@ 2003-04-10 20:03 Paul Dubuc
  0 siblings, 0 replies; only message in thread
From: Paul Dubuc @ 2003-04-10 20:03 UTC (permalink / raw)
  To: gcc-help

The following simple example illustrates a problem with g++ 2.95.3 when
it tries to decide the "best" template function match:

#include <iostream>

class Base
{
     public:
	Base()           { }
	virtual ~Base () { }
} ;


class Derived : public Base
{
     public:
	Derived()   { }
	~Derived()  { }
} ;

class BasePtr {
   public:
     BasePtr(void)           {}
     BasePtr(const BasePtr&) {}

     BasePtr(Base*)
     {
	cout << "In * ctor" << endl;
     }

     BasePtr& operator=(const BasePtr&)
     {
	cout << "In const& assignment" << endl;
	return *this;
     }

     BasePtr& operator=(Base*)
     {
	cout << "In * assignment" << endl;
	return *this;
     }

     template <typename T> BasePtr& operator=(T*)
     {
	cout << "In template function T* assignment" << endl;
	return *this;
     }

     template <typename T> BasePtr& operator=(const T&)
     {
	cout << "In template function const T& assignment" << endl;
	return *this;
     }
} ;

int
main(int,char**)
{
     Base x;
     Derived d;

     BasePtr px = &d;  // Should call * ctor
     px = &d;          // Should call T* assignment
     px = d;           // Should call const T& assignment

     return 0;
}

When compiled with g++ 2.95.3, the following error message is generated:

$ .../2.95.2/bin/g++ bug.cc -o bug
bug.cc: In function `int main(int, char **)':
bug.cc:60: ambiguous overload for `BasePtr & = Derived *'
bug.cc:29: candidates are: class BasePtr & BasePtr::operator =(const 
BasePtr &)
bug.cc:35:                 class BasePtr & BasePtr::operator =(Base *)
bug.cc:41:                 class BasePtr & BasePtr::operator 
=<Derived>(Derived *)
bug.cc:47:                 class BasePtr & BasePtr::operator =<Derived 
*>(Derived *const &)

However, when compiled with g++ 3.2.1, it compiles and executes correctly:


$ .../3.2.1/bin/g++ bug.cc -o bug -Wno-deprecated
$ ./bug
In * ctor
In template function T* assignment
In template function const T& assignment

At the moment, we are not in a position to upgrade to 3.2.1.  Short of 
changing
line 60 from

     px = &d;          // Should call T* assignment

to

     px = static_cast<Base*>(&d);  // Should call * assignment

is there any known workaround for this problem with 2.95.3?

Thanks,
-- 
Paul M. Dubuc

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-04-10 20:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-10 20:03 g++ 2.95.3 template function bug Paul Dubuc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).