Does
std::make_unique<double[]>(1000)
allways value-initialize the elements? Well, I looked into the implementation and clang
as well as g++
are using
new T[size]()
doing value-initialization.
But I can't find that a conforming implementation of C++14 / 17 has to do this.
You can use make_unique to create a unique_ptr to an array, but you cannot use make_unique to initialize the array elements.
A little late, but make_unique can itself throw according to cppreference: make_unique "may throw std::bad_alloc or any exception thrown by the constructor of T.
make_unique prevents the unspecified-evaluation-order leak triggered by expressions like foo(unique_ptr<X>(new X), unique_ptr<Y>(new Y)) . (Following the advice "never say new " is simpler than "never say new , unless you immediately give it to a named unique_ptr ".)
Nullability - a scoped_ptr or unique_ptr can be null, a value object can never be. Polymorphism - a value object is always exactly its static type, but you can substitute in different derived types for a unique_ptr. The previously-held object is automatically destroyed when you do this.
If the standard library is conforming to C++14 then yes it has to do this (use new T[size]
). From C++14 §20.8.1.4[unique.ptr.create]/4:
template <class T> unique_ptr<T> make_unique(size_t n);
- Returns:
unique_ptr<T>(new remove_extent_t<T>[n]())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With