为了避免大量不必要的复制,我试图将 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)