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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:35:43+00:00 2026-05-18T05:35:43+00:00

As a followup to Class containing auto_ptr stored in vector , I believe the

  • 0

As a followup to Class containing auto_ptr stored in vector, I believe the unstated conclusion there is that it is OK to use classes with an auto_ptr member as container elements, so long as both copy construction and copy assignment are user-defined (so as not to invoke the auto_ptrs copy constructor and copy assignment). Is there anything about this that violates the Standard Library requirements?

Please tell me if there is anything wrong with the following, as I’d like to start doing it idiomatically:

#include <memory>
#include <algorithm>

class Y { /* ... */ };
class Z : public Y { /* ... */ };

class X {
    public:
        X() : ap(new Z()) {}
        X(const X& other) : ap(other.ap->clone()) {}
        X& operator=(X other) { swap(other); return *this; } // any problem with swap?
        void swap(X& other) { std::swap(ap, other.ap); }
        // note no destructor necessary
    private:
        std::auto_ptr<Y> ap; // Y provides clone()
};

Note that one thing I’ve realized is that with this approach, Y‘s class definition must be in scope for X‘s definition (as opposed to just forward-declared when a “raw” pointer to Y is used). This is because when the auto_ptr is instantiated, its destructor must call delete on a Y, and so Y cannot be an incomplete type. I suppose this has to be weighed against the value of RAII in the case that class X managed several resources (has several auto_ptr members). Any thoughts on this?

I think this technique is great overall, to eliminate a lot of tricky code for classes that may have to construct/destruct/copy potentially several resources (single responsibility principal). I’m just wondering if auto_ptr serves the purpose, or if it doesn’t work here because of some subtle language/Standard Library requirement.

Please note that I’m aware of other types of smart pointers, but I’m implementing a library interface, and I don’t want to impose any requirements for Boost or TR1 on my clients. Any lecture on the use of auto_ptr in general will be downvoted!

  • 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-18T05:35:44+00:00Added an answer on May 18, 2026 at 5:35 am

    Using auto_ptr when the destructor is out of scope does cause undefined behavior. MSVC warns about it. GCC doesn’t.

    You can make it work, but only if the destructor is not defined as an inline function.

    The unique_ptr and scoped_ptr fix this problem. At least with those you are prevented from writing undefined code by accident.

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

Sidebar

Related Questions

No related questions found

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.