C++11的shared_ptr任务
C++11's shared_ptr assignment
我有以下代码:
#include <memory>
int main(void)
{
std::shared_ptr<int> currInt(nullptr);
std::shared_ptr<int> newInt(new int);
currInt = newInt;
return 0;
}
这在c++ 11中是无效的(在草案版本中是有效的),赋值构造函数现在使用move语义。有些事我不明白。
谁能解释一下我如何修改上面的代码使它…工作吗?
有shared_ptr
的复制构造函数,否则shared_ptr
有什么意义?
OP的clang链接是说,如果只定义了move赋值操作符,那么复制构造函数将被隐式删除,使shared_ptr
不能正确地。这也可以在Boost更改集中看到,其中显式添加了复制赋值操作符以纠正错误。
您可以在§20.7.2.2.3[util.smartptr.shared.assign]/1—3中找到shared_ptr
的复制赋值操作符。
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);
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 有没有任务栏API可以立即应用注册表更改
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 如何创建线程序列以按照启动顺序执行任务?
- C++一个线程如何正确通信其任务已完成?
- 在线程之间拆分任务总是值得的吗?
- 递归求和任务的错误答案
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为共享 ptr 向量实现复制 c'tor?
- C++ 任务流库入门
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 在程序运行时监视 VxWorks 中的任务 CPU 利用率
- 引用 std::shared:ptr 以避免引用计数
- 编写一个读取五个整数并执行一些任务的C++程序
- 任务内部的 OpenMP 任务循环
- 不知道如何在家庭作业任务中实现一件事