Why use std::make_unique in C++17?

后端 未结 6 1917
旧巷少年郎
旧巷少年郎 2020-12-23 10:55

As far as I understand, C++14 introduced std::make_unique because, as a result of the parameter evaluation order not being specified, this was unsafe:



        
6条回答
  •  梦毁少年i
    2020-12-23 11:23

    You're right that the main reason was removed. There are still the don't use new guidelines and that it is less typing reasons (don't have to repeat the type or use the word new). Admittedly those aren't strong arguments but I really like not seeing new in my code.

    Also don't forget about consistency. You absolutely should be using make_shared so using make_unique is natural and fits the pattern. It's then trivial to change std::make_unique(param) to std::make_shared(param) (or the reverse) where the syntax A requires much more of a rewrite.

提交回复
热议问题