PDA

View Full Version : QSet Move Insertion of Explicitly Shared Objects (C++11 & QSharedData Paradigm)



MacFreak84
4th September 2012, 01:59
Hello World,

So I am trying to use QExplicitlySharedDataPointer and QSharedData to create classes that can be stored in a QSet using explicit data sharing for memory management. Each class contains a copy and move constructor and operator overloads for move and copy assignment. I have a unit test that verifies my code, and all everything works as I expect it to.

The issue I am having is that when one of these objects is inserted into QSet, QSet duplicates the object when adding it to the set using the object's copy constructor instead of the move constructor. I want the object's data to be "moved into" the set... not copied in. I have tried using QSet<Object>.insert( std::move( object ) ), but no matter what I do the copy constructor is called instead of the move constructor. I am using Qt 4.8.1 and the newest Qt Creator release for Linux - GCC 4.7 compiler with the -std=c++11 compiler flag set.

Is this possible with Qt 4.8.1? I really don't want to have to roll out my own collection classes for this project.

Thank you for your help.
The MacFreak84

wysota
8th September 2012, 06:29
The issue I am having is that when one of these objects is inserted into QSet, QSet duplicates the object when adding it to the set using the object's copy constructor instead of the move constructor.
That's the correct behaviour, if you ask me...


I want the object's data to be "moved into" the set... not copied in. I have tried using QSet<Object>.insert( std::move( object ) ), but no matter what I do the copy constructor is called instead of the move constructor.
How are you creating "object"?

Besides, if you have explicitly shared data, I don't see any serious benefits of "moving" the data. With explicit sharing you are only copying a pointer and bumping up the reference counter (1 or 2 machine instructions).