het*_*fan 16 c++ pointers shared-ptr c++11
目前我正在使用glib库中的一些函数.随着glib也来了gio.glib是一个C库,因此我需要删除一些我创建的结构.
对于我创建智能指针的许多对象,例如:
std::shared_ptr<GAsyncQueue> my_queue = std::shared_ptr<GAsyncQueue>(g_async_queue_create(), g_async_queue_unref);
Run Code Online (Sandbox Code Playgroud)
为此创建了一个指向a的共享指针GAsyncQueue,这可以安全地在其生命周期结束时销毁队列.
但是,当我从gio库获取一个我不应该释放的指针时遇到问题.在下面的代码中my_connection是一个GSocketClient,它实现了(在glib中)GIOStream.
std::shared_ptr<GInputStream> my_input_stream =
std::shared_ptr<GInputStream> (
g_io_stream_get_input_stream(G_IO_STREAM(my_connection.get()))
);
Run Code Online (Sandbox Code Playgroud)
因为GIOStream上的文档提到,g_io_stream_get_input_stream()不应释放获得的指针.那是因为它归my_connection实例所有.我想为destroy对象创建一个lamda,它是共享指针对象的第二个参数.例如auto deleter = [](GInputStream* ptr) {};,然后将lambda作为detroy函数提供给共享指针,但这感觉有点愚蠢.
Sev*_*eux 12
好吧,替代no-op删除器可能正在使用别名共享指针
template <class U> shared_ptr (const shared_ptr<U>& x, element_type* p) noexcept;
Run Code Online (Sandbox Code Playgroud)
它分享x,但在get()之后你会回来p.
你可能只是不需要一个std::shared_ptr.你可能甚至不需要指针.
在我阅读你的问题和评论时,我认为没有任何反对意见
auto& my_input_stream = *( g_io_stream_get_input_stream(G_IO_STREAM(my_connection.get())) )
Run Code Online (Sandbox Code Playgroud)
确实,指针允许可选数据.然而,它的确大多使用了错误的方式.有
void foo( type* ptr)
{
if (!ptr)
throw exception;
}
Run Code Online (Sandbox Code Playgroud)
通常没有意义.如果函数必须处理具体数据,则允许NULL参数仅在您担心提供该数据时才有用.否则,只需要(可能const)引用该对象.
智能指针很有用; 但它们仍然是指针.如果可能的话,完全避免它们会更好.
来自评论:
但是,必须始终初始化引用
绝对.从C++ 11开始,虽然我们已经得到了std::reference_wrapper它,但也可以重新定位并存储在容器中.
您可以使用不执行任何操作的删除器类型,但需要将其作为参数传递给shared_ptr构造函数
struct DoNothing {
template <typename T>
void operator()(T*) const noexcept { }
};
Run Code Online (Sandbox Code Playgroud)
创建时,shared_ptr您需要创建其中一个删除器并将其传递给构造函数(就像您使用lambda一样).您可以使用中间功能使自己更容易
template <typename T>
std::shared_ptr<T> non_deleting_shared_ptr(T* ptr) {
return {ptr, DoNothing};
}
auto my_input_stream =
non_deleting_shared_ptr(
g_io_stream_get_input_stream(G_IO_STREAM(my_connection.get()));
Run Code Online (Sandbox Code Playgroud)
然而,更大的问题是,当您不希望所有权成为其中一部分时,为什么要使用智能指针.你几乎肯定会因为一个人而变得更好GAsyncQueue*,除非你处于一种有时shared_ptr需要释放的情况.像数据成员一样?
| 归档时间: |
|
| 查看次数: |
2078 次 |
| 最近记录: |