Boost logo

Boost Users :

From: Steven Solie (ssolie_at_[hidden])
Date: 2005-12-03 10:46:16


I don't understand why the following program doesn't compile when
using smart_ptr but works fine with raw pointers:

#include <boost/shared_ptr.hpp>

class Base { public: virtual ~Base() {}; };
class Base1 : public Base { public: virtual ~Base1() {}; };
class Base2 : public Base { public: virtual ~Base2() {}; };
class Derived1 : public Base1 {};
class Derived2 : public Base2 {};

typedef boost::shared_ptr<Base> BasePtr;
typedef boost::shared_ptr<Base1> Base1Ptr;
typedef boost::shared_ptr<Base2> Base2Ptr;
typedef boost::shared_ptr<Derived1> Derived1Ptr;
typedef boost::shared_ptr<Derived2> Derived2Ptr;

void f(BasePtr ptr) {}
void f(Base1Ptr ptr) {}
void f(Base2Ptr ptr) {}

int main()
{
    Derived1Ptr d1(new Derived1);
    Derived2Ptr d2(new Derived2);

    f(d1);
    f(d2);
}

I see the following compile errors with GCC 3.4.4:
smart-test.cpp: In function `int main()':
smart-test.cpp:24: error: call of overloaded `f(Derived1Ptr&)' is ambiguous
smart-test.cpp:15: note: candidates are: void f(BasePtr)
smart-test.cpp:16: note: void f(Base1Ptr)
smart-test.cpp:17: note: void f(Base2Ptr)
smart-test.cpp:25: error: call of overloaded `f(Derived2Ptr&)' is ambiguous
smart-test.cpp:15: note: candidates are: void f(BasePtr)
smart-test.cpp:16: note: void f(Base1Ptr)
smart-test.cpp:17: note: void f(Base2Ptr)

If I remove the extra layer of inheritance it works as it should.

#include <boost/shared_ptr.hpp>

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

typedef boost::shared_ptr<Base> BasePtr;
typedef boost::shared_ptr<Derived1> Derived1Ptr;
typedef boost::shared_ptr<Derived2> Derived2Ptr;

void f(BasePtr ptr) {}

int main()
{
    Derived1Ptr d1(new Derived1);
    Derived2Ptr d2(new Derived2);

    f(d1);
    f(d2);
}

As I said earlier, doing the same with raw pointers resolves things
as would be expected in both cases.

Any ideas?

--Steven


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net