public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paul Dubuc <pdubuc@cas.org>
To: gcc-help@gcc.gnu.org
Subject: g++ 2.95.3 template function bug
Date: Thu, 10 Apr 2003 20:03:00 -0000	[thread overview]
Message-ID: <3E95CE18.1010007@cas.org> (raw)

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

                 reply	other threads:[~2003-04-10 20:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3E95CE18.1010007@cas.org \
    --to=pdubuc@cas.org \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).