如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针

How can I use unique pointers for an array of parameterized objects in C++ using Visual Studio 2017?

本文关键字:数组 对象 指针 唯一 参数 Studio Visual 何使用 2017 C++      更新时间:2023-10-16

考虑一个类,其默认构造函数将文件路径作为参数。

class Test
{
public:
Test(const std::string& filepath);
...
...
};

现在,我希望在VS2017中使用唯一指针创建并初始化一个Test对象数组。

int main()
{
std::unique_ptr<Test[]> m_Tests;
int testCount = 2;
std::string path1, path2;
m_Tests = std::make_unique<Test[]>(testCount);    // This line gives a compilation error
m_Tests[0] = std::make_unique<Test>(path1);
m_Tests[1] = std::make_unique<Test>(path2);
}

我怎样才能做到这一点?

g++9.2.0告诉我您缺少默认的构造函数,即没有参数的构造函数。添加这样的构造函数效果很好。如果这不是您想要的,您可以创建unique_ptr的数组,因此std::unique_ptr<std::unique_ptr<Test>[]>和之后手动初始化每个元素,类似于以下内容:

#include <memory>
#include <algorithm>
#include <iostream>
struct Test {
std::string str_;
Test(std::string const& str) : str_(str) { }
void print() { std::cout << str_ << 'n'; }
};
int main()
{
std::unique_ptr<std::unique_ptr<Test>[]> m_Tests;
int testCount = 2;
std::string path1{"a"}, path2{"b"};
m_Tests = std::make_unique<std::unique_ptr<Test>[]>(testCount);
std::array<std::string, 2> paths{path1, path2};
std::transform(paths.begin(), paths.end(), &m_Tests[0],
[](auto const& p) { return std::make_unique<Test>(p); });
for (int i = 0 ; i < testCount ; ++i) {
m_Tests[i]->print();
}
}

std::make_unique没有过载,因此您需要直接使用new

m_Tests.reset(new Test[testCount]{path1, path2});

但是,只有当testCount是常量表达式时,这才会编译,因此需要将定义int testCount = 2;更改为const intconstexpr int

如果testCount不是常量表达式,则需要为运行时testCount小于2的情况定义默认构造函数。

所以,实际上,你可能想忽略testCount,只需要推导数组大小:

m_Tests.reset(new Test[]{path1, path2});

如果你只使用std::vector:会容易得多

std::vector<Test> m_Tests;
//...
m_Tests.emplace_back(path1);
m_Tests.emplace_back(path2);

不如使用std::array,然后去掉testCount(或将其用作constexp(,代码如下所示。

class Test
{
public:
Test(const std::string& filepath){}
};
int main()
{
constexpr int testCount = 2;
std::unique_ptr<std::array<Test, testCount>> m_Tests;
std::string path1, path2;
m_Tests = std::make_unique<std::array<Test, testCount>>(std::array<Test, testCount>{path1,path2});
}