19
using namespace boost;

class A {};
class B : public A {};

class X {
  virtual shared_ptr<A> foo();
};

class Y : public X {
  virtual shared_ptr<B> foo();
};

The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around this, if there is one?

1

3 Answers 3

11

I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers.

When Y::foo returns shared_ptr<B> to a dynamic caller, it must be cast to shared_ptr<A> before use. In your case, a B* can (probably) simply be reinterpreted as an A*, but for multiple inheritance, you would need some magic to tell C++ about static_cast<A*>(shared_ptr<B>::get()).

3
  • Hmm, "fundamentally" covariance depends on conversion, and perhaps should work with implicitly convertible types. But that still wouldn't apply to shared_ptr. Aug 16, 2012 at 5:51
  • "But that still wouldn't apply to shared_ptr" I don't get it: are shared_ptr not convertible?
    – curiousguy
    May 28, 2013 at 13:50
  • @curiousguy Oh, oops it is. Still just a hypothetical though. May 29, 2013 at 7:21
4

Not directly, but you can fake it by making the actual virtual functions inaccessible from outside the class and wrapping the virtual function call into a non-virtual function. Downside is that you'll have to remember to implement this wrapper function on each derived class. But you could get around this by puting both the virtul function declaration and the wrapper into the macro.

using namespace boost; // for shared_ptr, make_shared and static_pointer_cast.

// "Fake" implementation of the clone() function.
#define CLONE(MyType) \
    shared_ptr<MyType> clone() \
    { \
        shared_ptr<Base> res = clone_impl(); \
        assert(dynamic_cast<MyType*>(res.get()) != 0); \
        return static_pointer_cast<MyType>(res); \
    }

class Base 
{
protected:
    // The actual implementation of the clone() function. 
    virtual shared_ptr<Base> clone_impl() { return make_shared<Base>(*this); }

public:
    // non-virtual shared_ptr<Base> clone();
    CLONE(Base)
};

class Derived : public Base
{
protected:
    virtual shared_ptr<Base> clone_impl() { return make_shared<Derived>(*this); }

public:
    // non-virtual shared_ptr<Derived> clone();
    CLONE(Derived)
};


int main()
{
    shared_ptr<Derived> p = make_shared<Derived>();
    shared_ptr<Derived> clone = p->clone();

    return 0;
}
2
  • It has been a while since this was answered. Now that c++11 is out, is there a better way?
    – Cory-G
    Jul 28, 2014 at 23:12
  • @CoryBeutler. No, if you want to use shared_ptr. If you're willing to give up shared_ptr and are willing to use intrusive reference counting, then yes. In this case the functions would return raw pointers and to get automatic refcounting you just need to assign the result to a smart pointer (e.g. boost::intrusive_ptr). Jul 29, 2014 at 10:53
-1

I just return a bare pointer and wrap it immediately in the shared pointer.

1
  • You almost never want to wrap someone else's bare pointer to shared_ptr. Dec 21, 2016 at 4:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.