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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:24+00:00 2026-05-25T15:37:24+00:00

I learned that STL can forbid programmer putting an auto_ptr into a container. For

  • 0

I learned that STL can forbid programmer putting an auto_ptr into a container. For example following code wouldn’t compile:

    auto_ptr<int> a(new int(10));
    vector<auto_ptr<int> > v;
    v.push_back(a);

auto_ptr has the copy constructor, why this code can even compile?

  • 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-25T15:37:25+00:00Added an answer on May 25, 2026 at 3:37 pm

    Looking at the definition of std::auto_ptr:

    namespace std {
    
        template <class Y> struct auto_ptr_ref {};
    
    
        template <class X>
        class auto_ptr {
        public:
            typedef X element_type;
    
            // 20.4.5.1 construct/copy/destroy:
            explicit           auto_ptr(X* p =0) throw();
                               auto_ptr(auto_ptr&) throw();
            template <class Y> auto_ptr(auto_ptr<Y>&) throw();
    
            auto_ptr&                      operator=(auto_ptr&) throw();
            template <class Y> auto_ptr&   operator=(auto_ptr<Y>&) throw();
            auto_ptr&                      operator=(auto_ptr_ref<X>) throw();
    
            ~auto_ptr() throw();
    
            // 20.4.5.2 members:
            X&     operator*() const throw();
            X*     operator->() const throw();
            X*     get() const throw();
            X*     release() throw();
            void   reset(X* p =0) throw();
    
            // 20.4.5.3 conversions:
                                        auto_ptr(auto_ptr_ref<X>) throw();
            template <class Y> operator auto_ptr_ref<Y>() throw();
            template <class Y> operator auto_ptr<Y>() throw();
        };
    
    }
    

    Although there is a copy-constructor, it takes a reference to non-const. Temporaries may not bind to this, so the type is effectively prohibited from working inside containers in any place where temporaries are used; in addition, push_back accepts a reference to const, so due to const-correctness it’s impossible for the new, internal element to by copy-constructed from push_back‘s argument.

    (That Wikipedia page says that “because of its copy semantics, auto_ptr may not be used in STL containers that may perform element copies in their operations”; this doesn’t mean that containers magically examine the code inside the copy constructor to decide whether it wants to make the type work as an element type. Instead, it’s just about the function signature.)

    Anyway, std::auto_ptr is deprecated as of C++11 because, in the opinion of some, std::auto_ptr is silly. Sorry, std::auto_ptr.

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

Sidebar

Related Questions

I learned that auto-variables aren't initialized to zero. So the following code will behave
I learned that the Android Scripting Environment (ASE) supports python code. Can I take
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I've learned that MySQL can compress communication between servers and clients. Compression is used
Having just learned that many cpp features (including the stl vector class) do not
I recently learned that I can get hg log to print the history in
I've learned that every class byte code is been loaded to the memory once
I learned that I should unlock reverse order to lock order. For example. A.lock();
Through tutorials I had learned that you can define two variables in the same
I learned that if I compile main.cpp the compiler simply replaces all includes with

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.