在库的公共接口中使用 boost::shared_ptr [英] Using boost::shared_ptr in a library's public interface

查看:26
本文介绍了在库的公共接口中使用 boost::shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 C++ 库,可以提供给多个不同的客户.最近,我们从在公共接口中使用原始指针转换为使用 boost::sharedptr.正如您可能猜到的那样,这提供了一个巨大的好处,因为现在客户不再需要担心谁需要删除什么以及何时删除.当我们进行转换时,我认为这是正确的做法,但让我烦恼的是我们必须在我们的公共界面中包含来自第三方库的内容 - 通常,如果可以的话,您应该避免这种事情.我认为 boost 现在实际上是 C++ 语言的一部分,我们的用例要求客户端代码和库都保存指向对象的指针.然而最近我们的一个客户问我们是否可以在界面中切换到使用中性智能指针类,因为我们的库本质上是强迫他们使用特定版本的 boost——这一点我当然理解和欣赏.所以现在我想知道最好的行动方案是什么.我考虑了一下,想知道如何创建一个简单的智能指针类,它只包含一个真正的 boost 智能指针.但是,客户端可能会立即将其中一个填充到他们的 boost::sharedptr 风格中,然后我们将成为三个共享指针深 - 这可能是一个问题,也可能不是.无论如何,我很想听听社区关于解决这个问题的最佳方法的一些意见.

We have a C++ library that we provide to several different clients. Recently we made the switch from using raw pointers in the public interface to using boost::sharedptr instead. This has provided an enormous benefit, as you might guess, in that now the clients no longer have to worry about who needs to delete what and when. When we made the switch I believed it was the right thing to do, but it bothered me that we had to include something from a third-party library in our public interface - generally you avoid that kind of thing if you can. I rationalized it that boost was practically part of the C++ language now, and our use case requires that both the client code and the library hold pointers to the objects. However recently one of our clients has asked us if we could switch to using a neutral smart pointer class in the interface, because our library is essentially forcing them to a particular version of boost- a point which I certainly understand and appreciate. So now I am wondering what the best course of action might be. I have thought about it a little bit, and wondered about creating a simple smart pointer class that simply held a real boost smart pointer. But then the clients would probably immediately stuff one of those into their flavor of boost::sharedptr, and then we'd be three shared pointers deep - which might be a problem, or it might not. Anyway, I'd love to hear some opinions from the community about the best way to solve this problem.

我最初说的是所有权转移,但我应该指定 API 边界两侧的代码需要持有指向对象的指针.

I originally said transfer of ownership, but I should have specified that code on both sides of the API boundary need to hold a pointer to the object.

推荐答案

shared_ptr<> 是该语言的一部分,从 TR1 发布开始.请参阅:(TR1)

这篇关于在库的公共接口中使用 boost::shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆