返回shared_ptr时如何实现协变返回类型?
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();
};
返回类型不是协变的(因此它们也不合法),但如果我使用原始指针,它们就会是协变的。如果有的话,解决这个问题的普遍接受的习惯用法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为解决方案基本上是不可能的,因为协方差依赖于与智能指针不兼容的指针算术。
当
Y::foo
将shared_ptr
返回给动态调用者时,必须在使用前将其强制转换为shared_ptr
。在您的情况下,B*
可以(可能)简单地被重新解释为A*
,但对于多重继承,您需要一些魔法来告诉 C++ 关于static_cast(shared_ptr::get())
。I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers.
When
Y::foo
returnsshared_ptr<B>
to a dynamic caller, it must be cast toshared_ptr<A>
before use. In your case, aB*
can (probably) simply be reinterpreted as anA*
, but for multiple inheritance, you would need some magic to tell C++ aboutstatic_cast<A*>(shared_ptr<B>::get())
.不是直接的,但您可以通过使实际的虚拟函数无法从类外部访问并将虚拟函数调用包装到非虚拟函数中来伪造它。缺点是您必须记住在每个派生类上实现此包装函数。但是您可以通过将虚拟函数声明和包装器放入宏中来解决这个问题。
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.
我只是返回一个裸指针并将其立即包装在共享指针中。
I just return a bare pointer and wrap it immediately in the shared pointer.