对std::unique_ptr数组的赋值
assignment to an array of std::unique_ptr
struct MyStruct
{
int x = 0;
}
std::array<std::unique_ptr<MyStruct>, 10> Arr;
// Arr[0] = ?
将对象分配给这样的数组的语法是什么?我的推荐人。
费翔回答:
Arr[0].reset(new MyStruct);
雷米·勒博回答:
Arr[0] = std::make_unique<MyStruct>(); // Since C++14
或者
std::array<std::unique_ptr<MyStruct>, 10> Arr {
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>(),
std::make_unique<MyStruct>()
};
为了避免移动任务。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 唯一/共享 ptr 用于在数组超出范围后自动删除阵列
- 如何将函数 ptr 分配给函数 ptrs 数组(Arduino C++)
- 如何删除带有 int ptr 指向它的数组
- 动态2d数组:int(*ptr)[4]=new int[7][4];
- 有没有办法在数组上遍历shared_或任何"smart" ptr?
- 从数组的共享ptr访问共享ptr
- 分配const char *ptr数组参数?c++
- 按字母顺序排列字符串数组(甚至是*ptr)
- 使用ptr-to-ptr数组
- c++类的析构函数包含ptr到对象的数组