gpt4 book ai didi

c++ - 替代初始化唯一指针的 vector

转载 作者:行者123 更新时间:2023-11-30 01:40:47 25 4
gpt4 key购买 nike

<分区>

我需要一种方法来获取从函数返回的“std::unique_ptr”,并在初始化列表中使用它来初始化它们的 std::vector。我愿意接受任何涉及用逗号分隔列表初始化 std::vector 的解决方案(即可变参数函数返回 vector ),但此过程需要使用移动语义来获取返回的 ´ std::unique_ptr´ 数据。我遇到的问题可以在下面的代码中看到:

#include <vector>
#include <memory>

std::unique_ptr<int> create(int value) {
return std::make_unique<int>(value);
}

int main() {
std::vector<std::unique_ptr<int>> myVec = { create(1), create(2), create(3) }; //this results in an error
return 0;
}

Error Info: C2280 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0 737

我正在使用 microsoft visual studio 2015 编译程序。

编辑:正如我之前所说,我需要一些只涉及逗号分隔的唯一指针列表的解决方案,我似乎无法弄清楚如何使用可变参数来告诉我 std::initializer_list 之类的参数数量可以做到.我可以使用一些模板或宏黑客来实现这一目标吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com