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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With