使用具有以下初始化的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
}
对应部分为:
// 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。