小编ant*_*iok的帖子

是否可以使用 std::make_unique 对动态数组进行大括号初始化?

通常,我们可以使用动态分配的大括号初始化来创建一个数组

int* arr = new int[5]{1,1,2,4,5};
Run Code Online (Sandbox Code Playgroud)

但这可以使用智能指针,特别是使用 std::make_unique 吗?我尝试过以下方法:

unique_ptr<int[]> arr(5) {1,1,2,4,5};
unique_ptr<int[]> arr = make_unique<int[]>(5){1,1,2,4,5};
unique_ptr<int[]> arr = make_unique<int[]>({1,1,2,4,5});
Run Code Online (Sandbox Code Playgroud)

但没有用,而且我认为使用智能指针甚至不可能实现这一点。任何有关如何对智能指针使用大括号初始化的建议将不胜感激。

是的,我知道std::vector,但希望是否有其他方法。

c++ initialization smart-pointers unique-ptr c++11

5
推荐指数
1
解决办法
329
查看次数

标签 统计

c++ ×1

c++11 ×1

initialization ×1

smart-pointers ×1

unique-ptr ×1