C++11 清除shared_ptr 时,我应该使用reset 还是set 为nullptr?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16151550/
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
C++11 When clearing shared_ptr, should I use reset or set to nullptr?
提问by user1930581
I have a question about C++11 best practices. When clearing a shared_ptr, should I use the reset()
function with no parameter, or should I set the shared_ptr
to nullptr
? For example:
我有一个关于 C++11 最佳实践的问题。清除 shared_ptr 时,我应该使用reset()
不带参数的函数,还是应该将其设置shared_ptr
为nullptr
?例如:
std::shared_ptr<std::string> foo(new std::string("foo"));
foo.reset();
foo = nullptr;
Is there any real difference, or are there advantages/disadvantages to either approach?
有什么真正的区别,或者这两种方法都有优点/缺点吗?
回答by Andy Prowl
Is there any real difference, or are there advantages/disadvantages to either approach?
有什么真正的区别,或者这两种方法都有优点/缺点吗?
The two alternatives are absolutely equivalent, in the sense that the second form (foo = nullptr
) is defined in terms of the first one. Per Paragraph 20.7.1.2.3/8-10 of the C++11 Standard:
这两种选择是绝对等价的,因为第二种形式 ( foo = nullptr
) 是根据第一种形式定义的。根据 C++11 标准的第 20.7.1.2.3/8-10 段:
unique_ptr& operator=(nullptr_t) noexcept;
8 Effects:
reset()
.9 Postcondition:
get() == nullptr
10 Returns:
*this
.
unique_ptr& operator=(nullptr_t) noexcept;
8效果:
reset()
。9后置条件:
get() == nullptr
10回报:
*this
。
Therefore, just choose the one which makes its intent clearest for you. Personally, I prefer:
因此,只需选择一个对您来说最清楚其意图的。就个人而言,我更喜欢:
foo = nullptr;
Because it makes it more evident that we want the pointer to be null. As a general advice, however, try to minimize the situations where you need to explicitlyreset a smart pointer.
因为它更明显地表明我们希望指针为空。但是,作为一般建议,尽量减少需要显式重置智能指针的情况。
Besides, rather than using new
:
此外,而不是使用new
:
std::shared_ptr<std::string> foo(new std::string("foo"));
Consider using std::make_shared()
when possible:
std::make_shared()
尽可能考虑使用:
auto foo = std::make_shared<std::string>("foo");
回答by Walter
I would prefer reset()
as it signals the intent. However, try to write your code such that you do not need to explicitlyclear a shared_ptr<>
, i.e. ensure that a shared_ptr<>
goes out of scope when you would otherwise clear it.
我更喜欢,reset()
因为它表明了意图。但是,请尝试编写您不需要明确清除 a 的代码shared_ptr<>
,即确保 ashared_ptr<>
超出范围,否则您将清除它。
回答by r0ng
They have a bit different if you use https://godbolt.org/to check
by using gcc(7.2)foo.reset();
generates assembly code
如果您使用https://godbolt.org/
通过使用 gcc(7.2)foo.reset();
生成汇编代码进行检查,它们会有所不同
lea rax, [rbp-32]
mov rdi, rax
call std::__shared_ptr<int, (__gnu_cxx::_Lock_policy)2>::reset()
however,
foo = nullptr;
generates
然而,
foo = nullptr;
产生
lea rax, [rbp-16]
mov esi, 0
mov rdi, rax
call std::shared_ptr<int>::shared_ptr(decltype(nullptr))
lea rdx, [rbp-16]
lea rax, [rbp-32]
mov rsi, rdx
mov rdi, rax
call std::shared_ptr<int>::operator=(std::shared_ptr<int>&&)
lea rax, [rbp-16]
mov rdi, rax
call std::shared_ptr<int>::~shared_ptr()
It creates a shared pointer with nullptr, assign the newly created object to the variable and calls destructor to destory string.
它创建一个带有 nullptr 的共享指针,将新创建的对象分配给变量并调用析构函数来销毁字符串。
Since I don't know how to check what happened in the function reset(). Can not see which is faster.
因为我不知道如何检查函数 reset() 中发生了什么。看不出哪个更快。
回答by Xaltar
Generally, smart pointers can handle themselves. But if you need a solution, the reset()
is, in my opinion, your best bet.
通常,智能指针可以自行处理。但如果你需要一个解决方案,reset()
在我看来,这是你最好的选择。