Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store unique_ptr in a QList of QPairs?

To avoid a lot of unnecessary copying I'm trying to store unique_ptr's in a list of pairs. I'm using a simple class Test which takes a QString;

I'm using VS2013 with 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

Because of failure I tried:

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

Because of failure I changed completely to STL containters:

std::list<std::pair<unique_ptr<Test>, unique_ptr<Test>>> list;
auto pair = make_pair(std::move(a), std::move(b)); // Succes
list.push_back(std::move(pair)); // Succes

This works. Is my conclusion correct that these Qt containers don't support move semantics and I have to use STL instead?

like image 631
Jeruntu Avatar asked Nov 10 '22 17:11

Jeruntu


1 Answers

std::unique_ptr is not copyable, so nope for Qt containers.

Qt containers where created way before std::move (or even std::string) became a thing.

Maybe there will be better support in Qt6. It is poised to break a few things to integrate better with modern C++.

OTOH, you might as well use the std-containers if they work for you, unless you had some specific use case in mind?

like image 145
Macke Avatar answered Nov 15 '22 06:11

Macke