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

后端 未结 6 1909
旧巷少年郎
旧巷少年郎 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条回答
  •  北海茫月
    2020-12-23 11:31

    The reason is to have shorter code without duplicates. Compare

    f(std::unique_ptr(new MyClass(param)), g());
    f(std::make_unique(param), g());
    

    You save MyClass, new and braces. It costs only one character more in make in comparison with ptr.

提交回复
热议问题