std :: shared_ptr为空但不为null [英] std::shared_ptr which is empty but not null
问题描述
http://www.cplusplus.com/reference/memory/shared_ptr/说
不拥有任何指针的shared_ptr称为空的shared_ptr.指向无对象的shared_ptr称为null shared_ptr,并且不得取消引用.请注意,尽管一个空的shared_ptr不一定是一个null shared_ptr,而一个null的shared_ptr不一定是一个空shared_ptr.
A shared_ptr that does not own any pointer is called an empty shared_ptr. A shared_ptr that points to no object is called a null shared_ptr and shall not be dereferenced. Notice though that an empty shared_ptr is not necessarily a null shared_ptr, and a null shared_ptr is not necessarily an empty shared_ptr.
我是否可以创建一个空的std :: shared_ptr,即一个不拥有任何东西但不为null的东西(即包含有效载荷)?
Am I able to create an empty std::shared_ptr, i.e. one which does not own anything but which is not null, i.e. contains payload?
用例是将旧版"代码与现代指针结合起来:鉴于 foo
的调用语法不会更改,
The usecase is to marry "legacy" code with the modern pointers: Given that foo
's call syntax is not to be changed,
void foo(const MyClass* p) {
if (p == nullptr) {
auto defaultObject = std::make_shared<MyClass>("some", "parameters");
defaultObject->doSomething();
...
defaultObject->doSomethingElse();
// The default object must be destroyed again
} else {
p->doSomething();
...
p->doSomethingElse();
// p must not be destroyed, its memory is managed somewhere else
}
}
是否有 doNotOwnButStillPointTo()
的实现,该实现允许这样做:
is there an implementation for doNotOwnButStillPointTo()
which allows this:
void foo(const MyClass* p) {
std::shared_ptr<MyClass> wrapper;
if (p == nullptr) {
// Create a default object
wrapper = std::make_shared<MyClass>("some", "parameters");
} else {
wrapper = doNotOwnButStillPointTo(p);
}
wrapper->doSomething();
...
wrapper->doSomethingElse();
// p mus not be destroyed, the default object (if created) shall be
}
或者,为了不迷恋 XY问题,是否有其他可用的智能指针或没有?全部吗?
Or, to not fall for the XY-Problem, is there a different smart pointer available or none at all?
- 不过,我想补充一句,
std :: make_shared< MyClass>("some","parameters")
行实际上是对一个更复杂的函数的调用,该函数创建了shared_ptr.我想保留此功能并使用其结果
- However, I want to add, that the line
std::make_shared<MyClass>("some", "parameters")
is actually a call to a more complex function which creates a shared_ptr. I'd like to retain this function and use its result
推荐答案
shared_ptr
具有一个 deleter .这是销毁基础对象的多态过程.您可能有一个空的删除器:
The shared_ptr
has a deleter. This is a polymorphic procedure of destroying the underlying object. You may have an empty deleter:
void foo(const MyClass* p) {
std::shared_ptr<MyClass> wrapper;
if (p == nullptr) {
// Create a default object
wrapper = std::make_shared<MyClass>("some", "parameters");
} else {
wrapper = std::shared_ptr<MyClass>(p, [](MyClass*){});
}
wrapper->doSomething();
...
wrapper->doSomethingElse();
// p mus not be destroyed, the default object (if created) shall be
}
但是,这会导致设计不良.回到问题XY:目的是什么?有人可能向您传递一个对象作为原始指针(但可能会传递nullptr).您希望创建一个本地,以防提供nullptr而不是实际对象.您可能希望防止内存泄漏.好吧.
This however leads to a bad design. Returning back to the ProblemXY: what is the purpose? Someone may pass you an object as a raw pointer (but may pass a nullptr). You wish to create a local in case the nullptr is provided instead of a real object. And you probably wish to prevent memory leak. Ok.
void foo(const MyClass* p) {
std::shared_ptr<MyClass> local;
if (p == nullptr) {
// Create a default object
local = std::make_shared<MyClass>("some", "parameters");
p = local.get();
}
// p is always valid, local will always be destroyed (if exists)
p->doSomething();
...
p->doSomethingElse();
}
这篇关于std :: shared_ptr为空但不为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!