如何返回唯一指针的数组/向量
How to return an array / vector of unique pointers?
尝试使用本机数组、std::array 和 std::vector 编写以下代码
typedef unique_ptr<int> UPtr;
UPtr[] f() // wrong, how to return a native array?
{
UPtr a[] = { UPtr(new int(1)), UPtr(new int(2)) };
return std::move(a);
}
std::array<UPtr, 2> g()
{
std::array<UPtr, 2> a = { UPtr(new int(1)), UPtr(new int(2)) }; // compile ok but run wrong, 1 and 2 are not assigned
return std::move(a); // wrong, call a deleted function
}
std::vector<UPtr> h()
{
std::vector<UPtr> a = { UPtr(new int(1)), UPtr(new int(2)) }; // wrong, call a deleted function
return std::move(a);
}
都失败了。这里有很多问题。如何解决它们?多谢。
由于std:array<T, N>
是一个聚合,因此在其成员允许的范围内,它是可复制或可移动的。这意味着一切都应该按预期工作。事实上,下面的代码在 Clang、libc++ 和 -std=c++1y
(后者为 std::make_unique
选择(以及 GCC 4.8 中使用 libstdc++ 编译得很好,如果你拼出make_unique
调用:
#include <memory>
#include <array>
std::array<std::unique_ptr<int>, 2> f()
{
std::array<std::unique_ptr<int>, 2> a = { {
std::make_unique<int>(1), std::make_unique<int>(2) } };
return a;
}
#include <iostream>
int main()
{
auto a = f();
std::cout << *a[0] << " " << *a[1] << "n";
for (auto const & p : f())
std::cout << *p << "n";
}
但请注意,大括号省略(特别是在直接列表初始化的上下文中(和仅可移动类型的聚合初始化是 C++11 的新功能,供应商支持的发展速度很慢,因此错误并不少见。
另请注意,std::vector<std::unique_ptr<int>>
的列表初始化(直接或复制(不起作用,因为该函数需要一个接受std::initializer_list
的构造函数调用,而该构造函数不能处理仅可移动的类型。相比之下,std::array
工作,因为在这种情况下,列表初始化是聚合初始化。
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空