C++ 检查 std::shared_ptr 中的 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22220512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for null in std::shared_ptr
提问by Shoe
I was wondering if i need to check whether sp
is null
before i use it.
Correct me if I am wrong but creating an alias will not increase the ref counter and therefore by entering into the method we are working with a shared pointer which we don't know if the embedded pointer has been reset before.. am I correct by assuming this?
我想知道在我使用它之前是否需要检查是否sp
是null
。如果我错了,请纠正我,但创建别名不会增加引用计数器,因此通过进入我们正在使用的共享指针的方法,我们不知道嵌入的指针之前是否已重置..我是否正确假设这个?
Class::MyFunction(std::shared_ptr<foo> &sp)
{
...
sp->do_something();
...
}
采纳答案by James Kanze
Most shared pointers are exactly like normal pointers in this respect. You have to check for null. Depending on the function, you may want to switch to using
大多数共享指针在这方面与普通指针完全一样。您必须检查是否为空。根据功能,您可能希望切换到使用
void myFunction( Foo const& foo );
, and calling it by dereferencing the pointer (which pushes the responsibility for ensuring that the pointer is not null to the caller).
,并通过取消引用指针来调用它(这将确保指针不为空的责任推给调用者)。
Also, it's probably bad practice to make the function take a shared pointer unlessthere are some special ownership semantics involved. If the function is just going to use the pointer for the duration of the function, neither changing it or taking ownership, a raw pointer is probably more appropriate, since it imposes less constraints on the caller. (But this really depends a lot on what the function does, and why you are using shared pointers. And of course, the fact that you've passed a non-const reference to the shared pointer supposes that you are going to modify it, so passing a shared pointer might be appropriate.)
此外,除非涉及一些特殊的所有权语义,否则让函数采用共享指针可能是不好的做法。如果函数只是在函数运行期间使用指针,既不更改指针也不获取所有权,则原始指针可能更合适,因为它对调用者施加的约束较少。(但这实际上在很大程度上取决于函数的作用,以及您为什么使用共享指针。当然,您已将非常量引用传递给共享指针这一事实假设您将要修改它,因此传递共享指针可能是合适的。)
Finally, different implementations of shared pointers make it
more or less difficult to check for null. With C++11, you can
use std::shared_ptr
, and just compare it to nullptr
naturally, as you'd expect. The Boost implementation is a bit
broken in this respect, however; you cannot just compare it to
0
or NULL
. You must either construct an empty
boost::shared_ptr
for the comparison, or call get
on it and
compare the resulting raw pointer to 0
or NULL
.
最后,共享指针的不同实现使得检查 null 或多或少变得困难。使用 C++11,您可以使用std::shared_ptr
,并将其与nullptr
自然进行比较,正如您所期望的那样。然而,Boost 的实现在这方面有点问题。你不能只是将它与0
或进行比较
NULL
。您必须boost::shared_ptr
为比较构造一个空
值,或者调用get
它并将结果原始指针与0
或进行比较NULL
。
回答by Shoe
You have to consider that std::shared_ptr
is overall still a pointer (encapsulated in a pointer like class) and that it can indeed be constructed to internally be nullptr
. When that happens, expressions like:
您必须考虑到std::shared_ptr
它总体上仍然是一个指针(封装在类似类的指针中)并且它确实可以在内部构造为nullptr
. 发生这种情况时,表达式如下:
ptr->
*ptr
leads to undefined behavior. So, yeah, if you are expecting the pointer to also be nullptr
, then you should check for its valuewith:
导致未定义的行为。所以,是的,如果您希望指针也是nullptr
,那么您应该检查它的值:
ptr != nullptr
or
或者
!ptr
(thanks to its operator bool).
(感谢它的操作符 bool)。
回答by Samuel
There is no point in passing a shared_ptr as reference.
将 shared_ptr 作为参考传递是没有意义的。
You can obtain the internal object via boost::shared_ptr<T>.get()
and check for nullptr
您可以通过获取内部对象boost::shared_ptr<T>.get()
并检查nullptr
Also relevant: move to std :)
也相关:移至 std :)
Edit: This is the implementation: http://www.boost.org/doc/libs/1_55_0/boost/smart_ptr/shared_ptr.hppAnd here is a SO thread about ref or no ref: Should I pass a shared_ptr by reference?
编辑:这是实现:http: //www.boost.org/doc/libs/1_55_0/boost/smart_ptr/shared_ptr.hpp这里有一个关于 ref 或 no ref 的 SO 线程:我应该通过引用传递 shared_ptr 吗?
It uses move semantics when Cx11 and copies two ints otherwise which is slower than passing a reference but when is somebody on this level of optimization?
它在 Cx11 时使用移动语义并复制两个整数,否则这比传递引用慢,但是何时有人处于这种优化级别?
回答by Angew is no longer proud of SO
There's no general answer to this question. You have to treat it just like any other pointer. If you don't know whether it's null, test. If you believe it to never be null, assert()
that it's not null and use it directly.
这个问题没有普遍的答案。你必须像对待任何其他指针一样对待它。如果您不知道它是否为空,请测试。如果您相信它永远不会为空,assert()
那么它就不是空的并直接使用它。
The fact that you have a reference to shared_ptr
, or even that you have a shared_ptr
, has no impact here.
您有对 的引用shared_ptr
,甚至您有shared_ptr
,这一事实在这里没有影响。