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 569649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:19:04+00:00 2026-05-13T13:19:04+00:00

auto_ptr_ref documentation here says this This is an instrumental class to allow certain conversions

  • 0

auto_ptr_ref documentation here says this

This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions.

Can somebody explain how auto_ptr_ref helps in achieving this. I just want to understand the auto_ptr class and its internals

  • 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-13T13:19:04+00:00Added an answer on May 13, 2026 at 1:19 pm

    It is rather confusing. Basically, auto_ptr_ref exists because the auto_ptr copy constructor isn’t really a copy constructor in the standard sense of the word.

    Copy constructors typically have a signature that looks like this:

    X(const X &b);
    

    The auto_ptr copy constructor has a signature that looks like this:

    X(X &b)
    

    This is because auto_ptr needs to modify the object being copied from in order to set its pointer to 0 to facilitate the ownership semantics of auto_ptr.

    Sometimes, temporaries cannot match a copy constructor that doesn’t declare its argument const. This is where auto_ptr_ref comes in. The compiler won’t be able to call the non-const version of the copy constructor, but it can call the conversion operator. The conversion operator creates an auto_ptr_ref object that’s just sort of a temporary holder for the pointer. The auto_ptr constructor or operator = is called with the auto_ptr_ref argument.

    If you notice, the conversion operator in auto_ptr that automatically converts to an auto_ptr_ref does a release on the source auto_ptr, just like the copy constructor does.

    It’s kind of a weird little dance that happens behind the scenes because auto_ptr modifies the thing being copied from.

    Random related tanget about C++0x and unique_ptr

    In C++0x, auto_ptr is deprecated in favor of unique_ptr. unique_ptr doesn’t even have a copy constructor and uses the new ‘move constructor’ which is explicit about the fact that it will modify the object being moved from and leave it in a useless (but still valid) state. Temporaries (aka rvalues) are explicitly always allowed to be arguments to a move constructor.

    The move constructor in C++0x has a number of other big benefits. It enables the standard STL containers to store unique_ptrs and do the right thing, as opposed to how auto_ptrs cannot be. It also mostly eliminates the need for the ‘swap’ function as the whole purpose of the swap function is usually to be a move constructor or move assignment operator that never throws.

    Which is the other expectation. The move constructor and move assignment operator (much like a destructor) are never supposed to throw.

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

Sidebar

Related Questions

I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not
I have class foo that contains a std::auto_ptr member that I would like to
I have a code which looks like this: class Parent { auto_ptr<Resource> ptr2Resc; public:
Does C++ standard guarantee that here would be no crash when returning auto_ptr 's
I have class MyForm which inherited from QMainWindow . Here's my code: std::auto_ptr<MyForm> pForm(new
I am not quite clear if auto_ptr will help me in this case: class
I was going through the auto_ptr documentation on this link auto_ptr There is something
I've a situation like this: class MyClass { private: std::auto_ptr<MyOtherClass> obj; public: MyClass() {
Reference here That destructor will also implicitly call the destructor of the auto_ptr object.
If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass);

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.