小编Jer*_*ntu的帖子

是否可以将 unique_ptr 存储在 QPairs 的 QList 中?

为了避免大量不必要的复制,我试图将 unique_ptr 存储在一对列表中。我正在使用一个简单的类 Test,它带有一个 QString;

我正在使用 VS2013 和 Qt5.4

using std::unique_ptr;

QList<QPair<unique_ptr<Test>, unique_ptr<Test>>> list;

auto a = std::make_unique<Test>("a");
auto b = std::make_unique<Test>("b");

// First make a pair
auto pair = qMakePair(std::move(a), std::move(b));      // Fails
// Error C2280 - attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)

由于失败,我尝试了:

QList<std::pair<unique_ptr<Test>, unique_ptr<Test>>> list;
auto pair = std::make_pair(std::move(a), std::move(b)); // Succes
list.append(std::move(pair)); // Fails
// Error C2280 - attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)

由于失败,我完全改为 STL 容器:

std::list<std::pair<unique_ptr<Test>, unique_ptr<Test>>> list;
auto pair = make_pair(std::move(a), std::move(b)); // …
Run Code Online (Sandbox Code Playgroud)

c++ qt stl unique-ptr c++11

5
推荐指数
1
解决办法
2543
查看次数

标签 统计

c++ ×1

c++11 ×1

qt ×1

stl ×1

unique-ptr ×1