make_unique 值是否初始化 char 数组

does make_unique value initializes char array

例如-

#include <memory>

int main(){
    const auto bufSize = 1024;
    auto buffer = std::make_unique<char[]>(bufSize);
}

这里的缓冲区是否已经填充了 '[=12=]' 个字符,或者我必须手动填充它以避免垃圾值。

可能的方法是什么,std::memset(&buffer.get(), 0, bufSize) 就足够了吗?

如果您不提供构造函数参数,则所有 make_* 函数都使用 value-initialization 作为类型。由于 make_unique 的 array-form 不接受任何参数,它将 zero-out 元素。

是的,所有元素都是value initialized by std::make_unique

The function is equivalent to:

unique_ptr<T>(new typename std::remove_extent<T>::type[size]())

value initialization

This is the initialization performed when a variable is constructed with an empty initializer.

Syntax

new T (); (2)

The effects of value initialization are:

3) if T is an array type, each element of the array is value-initialized;
4) otherwise, the object is zero-initialized.

然后对于 char 类型的每个元素,它们将是 value-initialized (zero-initialized) 到 '[=14=]'

根据cppreference,是的:

2) Constructs an array of unknown bound T. This overload only participates in overload resolution if T is an array of unknown bound. The function is equivalent to:

unique_ptr<T>(new typename std::remove_extent<T>::type[size]())
                                       value initialization ^

我指示的值初始化。