版本比较
Modern C++ 智能指针浅谈
正文
增加 2 行删除 1 行
112 行未改动全部展开113114115116117118119120121122123187 行未改动全部展开112 行未改动全部展开auto p1 = std::make_unique<int>(114514);// 使用 C++14 make_unique 创建一个 unique_ptrauto p2 = std::unique_ptr<int>(new int(1919810));// 直接使用 new 创建(不推荐)// std::unique_ptr 有意没有实现// operator=(unique_ptr&) 与 unique_ptr(unique_ptr&),// 使其无法复制auto p3 = std::move(p1); // 显式转移所有权// p1 == nullptr, p2 拥有资源std::cout /*<< *p1 << " "*/ << *p2 << " " << *p3;// 若使用 p1 则会出现错误187 行未改动全部展开112 行未改动全部展开113114115116117118119120121122123124187 行未改动全部展开112 行未改动全部展开auto p1 = std::make_unique<int>(114514);// 使用 C++14 make_unique 创建一个 unique_ptrauto p2 = std::unique_ptr<int>(new int(1919810));// 直接使用 new 创建(不推荐)// std::unique_ptr 有意显式删除了// operator=(unique_ptr&) 与 unique_ptr(unique_ptr&),// 使其无法复制// 感谢 gtafics dalao 指出了问题所在auto p3 = std::move(p1); // 显式转移所有权// p1 == nullptr, p2 拥有资源std::cout /*<< *p1 << " "*/ << *p2 << " " << *p3;// 若使用 p1 则会出现错误187 行未改动全部展开