I am experimenting with smart pointers and try to exchange a few shared_ptr's with unique_ptr's in my project to see if I can gain little performance improvements.
I have the following code snippt:
// --- probably can be replaced with with std::copy ...
if (j != sentences.size()) {
// also gives me an error if I initialize vector with a size.
std::vector<unique_ptr<StemmedSentence> > newSentences(j);
int base = 0;
for (size_t i = 0; i < j; i++) {
newSentences[base++] = std::move(sentences[i]); // std::move ??
}
sentences = newSentences; // std::move, i guess not !?
}
I copy a vector of pointers into a new vector (in this case a vector of unique_ptr's). Using shared_ptr, this is no problem at all. As I understand, shared pointer uses the implicit assignment, all is working fine. With unique_ptr though, I need to overload the assignment operator (to transfer ownership) and this gives me quiet a headache.
I created this assignment:
ArrayStemmedSnippet& operator=(const ArrayStemmedSnippet& other) {
if (this != &other) {
sentences.clear();
std::vector<unique_ptr<StemmedSentence> >::const_iterator it;
for (it = other.sentences.begin(); it != other.sentences.end(); ++it) {
sentences.push_back(std::unique_ptr< StemmedSentence >(*it ?
*it : std::unique_ptr< StemmedSentence >())); // --- compiler error
}
return (*this);
}
}
The pointers to copy can contain "Null" pointers. I try to check this in the for loop and copy either the pointer or an empty unique_ptr instance (a nullptr). That line marked with compiler error gives me a problem, I can not figure out the correct syntax. Also I think I have to create a new pointer (clone) if I copy a non-null object. Can somebody please give me a hint on that?
I found a similar post Handling smart pointers in stl container, the answer from Howard Hinnant gave me the inspiration to give it a try. But I can not use that syntax, I have gcc version 4.4.3 on my testmachine.
I have no clue if I will gain more performace, like I said, I experiment. As far as I know I would have no ownership problems in my code, so I thought making a test with unique_ptr and skip the shared_ptr (with all its overhead) might be a little gain in performance. I have about 5 objects where I use shared_ptr, so I would give it a try.
If all this makes no sense, let me know if it is worth playing arround with uniqe_ptr at all.
Thanks in advance!