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

  • SEARCH
  • Home
  • 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 802979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:41:07+00:00 2026-05-14T23:41:07+00:00

I am confused with unique_ptr and rvalue move philosophy. Let’s say we have two

  • 0

I am confused with unique_ptr and rvalue move philosophy.

Let’s say we have two collections:

std::vector<std::auto_ptr<int>> autoCollection;
std::vector<std::unique_ptr<int>> uniqueCollection;

Now I would expect the following to fail, as there is no telling what the algorithm is doing internally and maybe making internal pivot copies and the like, thus ripping away ownership from the auto_ptr:

std::sort(autoCollection.begin(), autoCollection.end());

I get this. And the compiler rightly disallows this happening.

But then I do this:

std::sort(uniqueCollection.begin(), uniqueCollection.end());

And this compiles. And I do not understand why. I did not think unique_ptrs could be copied. Does this mean a pivot value cannot be taken, so the sort is less efficient? Or is this pivot actually a move, which in fact is as dangerous as the collection of auto_ptrs, and should be disallowed by the compiler?

I think I am missing some crucial piece of information, so I eagerly await someone to supply me with the aha! moment.

  • 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-05-14T23:41:07+00:00Added an answer on May 14, 2026 at 11:41 pm

    I think it’s more a question of philosophy than technic 🙂

    The underlying question is what is the difference between Move and Copy. I won’t jump into technical / standardista language, let’s do it simply:

    • Copy: create another identical object (or at least, one which SHOULD compare equal)
    • Move: take an object and put it in another location

    As you said, it is possible to implement Move in term of Copy: create a copy into the new location and discard the original. However there are two issues there. One is of performance, the second is about objects used for RAII: which of the two should have ownership ?

    A proper Move constructor solves the 2 issues:

    • It is clear which object has ownership: the new one, since the original will be discarded
    • It is thus unnecessary to copy the resources pointed to, which allows for greater efficiency

    The auto_ptr and unique_ptr are a very good illustration of this.

    With an auto_ptr you have a screwed Copy semantic: the original and the copy don’t compare equal. You could use it for its Move semantic but there is a risk that you’ll lose the object pointed to somewhere.

    On the other hand, the unique_ptr is exactly that: it guarantees a unique owner of the resource, thus avoiding copying and the inevitable deletion issue that would follow. And the no-copy is guaranteed at compile-time too. Therefore, it’s suitable in containers as long as you don’t try to have copy initialization.

    typedef std::unique_ptr<int> unique_t;
    typedef std::vector< unique_t > vector_t;
    
    vector_t vec1;                           // fine
    vector_t vec2(5, unique_t(new Foo));     // Error (Copy)
    vector_t vec3(vec1.begin(), vec1.end()); // Error (Copy)
    vector_t vec3(make_move_iterator(vec1.begin()), make_move_iterator(vec1.end()));
        // Courtesy of sehe
    
    std::sort(vec1.begin(), vec1.end()); // fine, because using Move Assignment Operator
    
    std::copy(vec1.begin(), vec1.end(), std::back_inserter(vec2)); // Error (copy)
    

    So you can use unique_ptr in a container (unlike auto_ptr), but a number of operations will be impossible because they involve copying which the type does not support.

    Unfortunately Visual Studio may be quite lax in the enforcement of the standard and also has a number of extensions that you would need to disable to ensure portability of the code… don’t use it to check the standard 🙂

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

Sidebar

Related Questions

I'm pretty confused on how to go about this. Say I have two columns
Lets say I have : (this class Is OK) class CCheckSystemEndianess { private: int
Bit confused here, I have an on-demand instance but do I get charged even
Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
Totally confused by this one... We have a WAMPServer installation set up, running a
I have two arrays a={a,b,c,d} b={1,2,3,4} now I want to create a 3rd array
I'm very confused with this wee little problem I have. I have a non-indexed
Very confused here, trying out the yuicompressor on a simple javascript file. My js
I confused when i want to take single pointer and when should i take
Why are the British confused about us calling bread rolls “biscuits” when they call bread rolls “puddings”?

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.