Dựa trên những câu trả lời trong thesequestionshere, tôi biết rằng chắc chắn nó được ưa thích sử dụng C++ 14 của std::make_unique
hơn để emplace_back(new X)
trực tiếp.push_back hoặc emplace_back với std :: make_unique
Điều đó nói rằng, nó là ưa thích để gọi
my_vector.push_back(std::make_unique<Foo>("constructor", "args"));
hoặc
my_vector.emplace_back(std::make_unique<Foo>("constructor", "args"));
Đó là, tôi nên sử dụng push_back
hoặc emplace_back
khi thêm một std::unique_ptr
xây dựng từ std::make_unique
?
==== EDIT ====
và tại sao? c: < - (nụ cười nhỏ)
Với đối số thực tế về loại mục của vectơ, 'push_back' và' emplace_back' nhất thiết phải làm như vậy. –
liên quan: http://htmlpreview.github.io/?https://github.com/HowardHinnant/papers/blob/master/insert_vs_emplace.html – dyp
@Praetorian Lỗi chính nhờ. – NHDaly