covariance of shared_ptr types
Hi, in C++ it's possible to write this: class A {}; class B: public A {}; class X { virtual A* f() { ... }; }; class Y: public X { virtual B* f() { ... }; // B* is covariant from A* }; However it is NOT possible to write that with shared_ptr: class X { shared_ptr<A> f() { ... }; }; class Y: public X { virtual shared_ptr<B> f() { ... }; // Error: shared_ptr<B> is NOT covariant from shared_ptr<A> }; I don't know C++ standard very well (in fact I've never read it). So my question is: it's possible to adjust shared_ptr to allow such constructions? Thank you, Wasek [Non-text portions of this message have been removed]
--- In Boost-Users@yahoogroups.com, "Vaclav Vesely"
Hi,
in C++ it's possible to write this:
class A {};
class B: public A {};
class X { virtual A* f() { ... }; };
class Y: public X { virtual B* f() { ... }; // B* is covariant from A* };
However it is NOT possible to write that with shared_ptr:
class X { shared_ptr<A> f() { ... }; };
class Y: public X { virtual shared_ptr<B> f() { ... }; // Error: shared_ptr<B> is NOT covariant from shared_ptr<A> };
I don't know C++ standard very well (in fact I've never read it). So my question is: it's possible to adjust shared_ptr to allow such constructions?
I'm also not a C++ guru, but I think it requires that shared_ptr<A> be the parent of shared_ptr<B>. How it can be achieved? If it could be possible in C++, it then it was easy: template<typename T> class shared_ptr<T>: public T* { ... } but AFAIK it is not possible.
Vaclav Vesely wrote:
However it is NOT possible to write that with shared_ptr:
class X { shared_ptr<A> f() { ... }; };
class Y: public X { virtual shared_ptr<B> f() { ... }; // Error: shared_ptr<B> is NOT covariant from shared_ptr<A> };
I don't know C++ standard very well (in fact I've never read it). So my question is: it's possible to adjust shared_ptr to allow such constructions?
No, but you can use the usual pattern from the no-covariant-return days: class B { virtual shared_ptr<B> do_clone() const = 0; }; class X: public B { virtual shared_ptr<B> do_clone() const { return shared_ptr<X>(new X(*this)); } public: shared_ptr<X> clone() const { shared_ptr<X> px = dynamic_pointer_cast<X>(this->do_clone()); assert(px); return px; } }; It buys you some extra safety, too. You could also use a free function: template<class X> shared_ptr<X> clone(X const & x) { shared_ptr<X> px = dynamic_pointer_cast<X>(x.do_clone()); assert(px); return px; }
Peter, thank you for your suggesstion. It's smart. Wasek
-----Original Message----- From: Peter Dimov [mailto:pdimov@mmltd.net] Sent: Friday, February 14, 2003 12:11 PM To: Boost-Users@yahoogroups.com Subject: Re: [Boost-Users] covariance of shared_ptr types
Vaclav Vesely wrote:
However it is NOT possible to write that with shared_ptr:
class X { shared_ptr<A> f() { ... }; };
class Y: public X { virtual shared_ptr<B> f() { ... }; // Error: shared_ptr<B> is NOT covariant from shared_ptr<A> };
I don't know C++ standard very well (in fact I've never
read it). So
my question is: it's possible to adjust shared_ptr to allow such constructions?
No, but you can use the usual pattern from the no-covariant-return days:
class B { virtual shared_ptr<B> do_clone() const = 0; };
class X: public B { virtual shared_ptr<B> do_clone() const { return shared_ptr<X>(new X(*this)); }
public:
shared_ptr<X> clone() const { shared_ptr<X> px = dynamic_pointer_cast<X>(this->do_clone()); assert(px); return px; } };
It buys you some extra safety, too.
You could also use a free function:
template<class X> shared_ptr<X> clone(X const & x) {
shared_ptr<X> px = dynamic_pointer_cast<X>(x.do_clone()); assert(px); return px; }
------------------------ Yahoo! Groups Sponsor ---------------------~--> Get 128 Bit SSL Encryption! http://us.click.yahoo.com/FpY02D/vN2EAA/xGHJAA/EbFolB/TM
-------------------------------------------------------------- -------~->
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
participants (3)
-
dlux42 <dlux@spam.sch.bme.hu>
-
Peter Dimov
-
Vaclav Vesely