如何返回唯一指针的数组/向量

How to return an array / vector of unique pointers?

本文关键字:指针 数组 向量 唯一 何返回 返回      更新时间:2023-10-16

尝试使用本机数组、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工作,因为在这种情况下,列表初始化是聚合初始化。