Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:59:05+00:00 2026-06-09T09:59:05+00:00

I am experimenting with smart pointers and try to exchange a few shared_ptr’s with

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:59:07+00:00Added an answer on June 9, 2026 at 9:59 am

    The question makes little sense. The decision of using shared_ptr or unique_ptr should be taken based on whether ownership is shared or unique. The question is focusing on the details of forcing a change in the design that is probably not wanted.

    In the original design, multiple vectors holding shared_ptr and sharing the ownership of some other objects, but in the modified approach, because unique_ptr has unique ownership you will be forced to copy the objects. The side effects of the change are multiple: now you need to copy the objects (higher cost). Because the type is a base, you need to implement a clone() approach so that the objects are not sliced, that will also have a (minimal) cost. After the copies are made the vectors will no longer refer to shared objects, so that a changed performed through one of them will not be visible through the rest, changing the semantics.

    At this point, I can only start thinking whether you have profiled your code, and how much of the cost of the application is in the copying of the shared pointers, because that is not an expensive operation. My feeling is that you might be barking at the wrong tree, spending time unnecessarily and breaking your application in the process, with a much more obscure approach for nothing.

    Sorry to sound negative, but I feel that you should really think this through. unique_ptr is not a replacement to shared_ptr, unless shared_ptr was the wrong solution from the start (objects should not really be shared), but that does not seem the case, since in your current program you are copying the pointers to different containers, and thus sharing.


    If what you want is to compact the vector, you don’t need copies at all, or even coding:

    // [untested, but you can build on top of this]
    typedef std::vector< std::unique_ptr< StemmedSentence > > vector_t;
    
    vector_t v = ... // build the vector
    // compact:
    v.erase( std::remove( std::make_move_iterator( v.begin() ),
                          std::make_move_iterator( v.end() ),
                          std::unique_ptr<StemmedSentence>() 
                        ).base(),
             v.end() );
    

    But this should not be implemented as operator= on the enclosing type taking a constant reference. unique_ptr cannot share ownership, so if the pointers are moved to the new container, then the old container is going to be emptied (this could fit a moving operator=, but not one that promises not to modify the original container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been experimenting with the instagram-ruby-gem on and off for a few days - no
I'm experimenting with a bit of Scala gui programming (my first project in scala,
I've been experimenting with Electric Fence lately and I can't figure out how to
I'm experimenting with P2P on Flash, and I've come across a little hurdle that
I've been experimenting with the Android SDK over the past few days, in readiness
I've decided to start experimenting with Ruby to get out of my little Java
This is my first time experimenting with android services, so I am a little
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
Experimenting with android game development. I'm using PNG format for images, and was wondering
While experimenting with pixel shaders in WPF I decided to draw some pixels onto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.