简体   繁体   English

std :: make_unique和std :: unique_ptr之间的区别与内部新

[英]Differences between std::make_unique and std::unique_ptr with new internally

This question is a follow up to this question . 这个问题是一个跟进这个问题

The accepted answer states: 接受的答案指出:

make_unique is safe for creating temporaries whereas with explicit use of new you have to remember the rule about not using unnamed temporaries make_unique对于创建临时make_unique是安全的,而显式使用new则必须记住有关不使用未命名临时文件的规则

I get the rough idea what is meant by this and I understand the example given, but how exactly does this work internally? 我大致理解了这是什么意思,并且我理解了给出的示例,但是这在内部究竟是如何工作的? What do these functions do exactly and where is the difference? 这些功能究竟有什么作用,区别在哪里?

What do these functions do exactly and where is the difference? 这些功能究竟有什么作用,区别在哪里?

Nothing magical actually. 其实没什么魔术。 They just transform user code from this: 他们只是从中转换用户代码:

f( std::unique_ptr<T1>{ new T1 }, std::unique_ptr<T2>{ new T2 } );

Into this: 变成这个:

f( make_unique<T1>(), make_unique<T2>() );

Just to avoid the scenario where the compiler orders the actions like the following, because it may do so until C++14 (included): 只是为了避免编译器命令如下操作的情况,因为它可能会在C ++ 14(包括)之前执行:

  • new T1
  • new T2
  • build first unique_ptr 建立第一个unique_ptr
  • build second unique_ptr 建立第二个unique_ptr

If the second step ( new T2 ) throws an exception, the first allocated object has not yet been secured into a unique_ptr and leaks. 如果第二步( new T2 )引发异常,则第一个分配的对象尚未固定到unique_ptr并泄漏。

The make_unique function is actually simple to write, and has been overlooked for C++11. make_unique函数实际上很容易编写,而C ++ 11却忽略了它。 It has been easily introduced in C++14. 它已在C ++ 14中轻松引入。 Inbetween, everybody would write their own make_unique template, or google the one written by Stephen T. Lavavej. 在这make_unique之间,每个人都将编写自己的make_unique模板,或在Google上搜索由Stephen T. Lavavej编写的模板。

As StoryTeller commented, since C++17, this interleaving is no more allowed. 正如StoryTeller所评论的那样,从C ++ 17开始,不再允许这种交织。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std::make_unique 和 std::unique_ptr 与 new 的区别 - Differences between std::make_unique and std::unique_ptr with new 带有前向声明的 std::make_unique 和 std::unique_ptr 之间的奇怪行为 - Strange behavior between `std::make_unique` and `std::unique_ptr` with forward declaration 为什么std :: make_unique而不是std :: unique_ptr :: make? - Why std::make_unique instead of std::unique_ptr::make? std :: make_unique会发生什么 <T> ()分配给std :: unique_ptr <T> ? - What happens when std::make_unique<T>() assigns to std::unique_ptr<T>? 我怎样才能转换std :: make_unique <derived> ()到std :: unique_ptr <base> - How can I convert std::make_unique<derived>() to std::unique_ptr<base> 将make_unique指定为std :: move()是否为空unique_ptr? - Does assigning make_unique require std::move() to an empty unique_ptr? 为什么 C++ 需要 std::make_unique 而不是转发的 unique_ptr 构造函数? - Why does C++ need std::make_unique over forwarded unique_ptr constructor? 没有重载函数“ std :: make_unique”的实例与参数列表匹配,但可与unique_ptr构造函数一起使用 - no instance of overloaded function “std::make_unique” matches the argument list, but works with unique_ptr constructor std :: move和unique_ptr :: reset之间有什么区别? - what are the differences between std::move and unique_ptr::reset? unique_ptr,make_unique和多态 - unique_ptr, make_unique and polymorphism
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM