提问者:小点点

如何使用这些初始化创建unique_ptr?


使用具有以下初始化的std::make_unique创建std::unique_ptr的等效版本是什么?

int main()
{
    // Single object
    // 1
    new int; // Indeterminate value
    // 2 
    new int(); // Zero initialized
    // 3
    new int(7); // Initialized with 7

    // Arrays
    // 1
    new int[10]; // Indeterminate values
    // 2
    new int[10](); // All zero-initialized
    // 3
    new int[10]{ 7, 6, 5, 4 }; // Initialized with list, rest all zero


   }

共1个答案

匿名用户

对应部分为:

// Single object
// 1
new int; // Indeterminate value
std::make_unique_for_overwrite<int>(); // since C++20

// 2 
new int(); // Zero initialized
std::make_unique<int>();

// 3
new int(7); // Initialized with 7
std::make_unique<int>(7);

// Arrays
// 1
new int[10]; // Indeterminate values
std::make_unique_for_overwrite<int[]>(10); // Since C++20
// 2
new int[10](); // All zero-initialized
std::make_unique<int[]>(10);
// 3
new int[10]{ 7, 6, 5, 4 }; // Initialized with list, rest all zero
std::unique_ptr<int[]>{ new int[10]{ 7, 6, 5, 4 } };

std::make_unique_for_overwrite仅为C++20。