Joo*_*kia 1 c++ shared-ptr c++11
我有以下代码:
#include <memory>
int main(void)
{
std::shared_ptr<int> currInt(nullptr);
std::shared_ptr<int> newInt(new int);
currInt = newInt;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事实证明,这不是有效的C++ 11(它用于草稿版本),并且赋值构造函数现在使用移动语义.我不明白的东西.
有人可以解释我是如何修改上面的代码使它...工作?
有一个复制构造函数shared_ptr,否则有什么意义shared_ptr呢?
OP 的clang链接表示如果只定义了移动赋值运算符,则隐式删除复制构造函数,使得shared_ptr行为不正确.这也可以在Boost变更集中看到,其中明确添加了复制赋值运算符以纠正错误.
您可以shared_ptr在§20.7.2.2.3[util.smartptr.shared.assign]/1-3中找到复制赋值运算符.
shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3646 次 |
| 最近记录: |