请输入您要查询的百科知识:

 

词条 Auto ptr
释义

  1. Declaration

  2. Semantics

  3. See also

  4. References

  5. External links

{{DISPLAYTITLE:auto_ptr}}

auto_ptr is a class template that was available in previous versions of the C++ Standard Library (declared in the header file), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr class.

The auto_ptr template class describes an object that stores a pointer to a single allocated object that ensures that the object to which it points gets destroyed automatically when control leaves a scope.[1]

The C++11 standard made auto_ptr deprecated, replacing it with the unique_ptr class template.[2][3] auto_ptr was fully removed in C++17.[4]

For shared ownership, the shared_ptr template class can be used. shared_ptr was defined in C++11 and is available in the Boost library.[5]

Declaration

The auto_ptr class is declared in ISO/IEC 14882, section 20.4.5 as:

namespace std {

    template     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  auto_ptr(auto_ptr&) throw();
        auto_ptr&                      operator=(auto_ptr&) throw();        template  auto_ptr&   operator=(auto_ptr&) throw();        auto_ptr&                      operator=(auto_ptr_ref) 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) throw();        template  operator auto_ptr_ref() throw();        template  operator auto_ptr() throw();    };

}

Semantics

The auto_ptr has semantics of strict ownership, meaning that the auto_ptr instance is the sole entity responsible for the object's lifetime. If an auto_ptr is copied, the source loses the reference. For example:

  1. include
  2. include

using namespace std;

int main(int argc, char **argv)

{
    int *i = new int;    auto_ptr x(i);    auto_ptr y;        y = x;        cout << x.get() << endl; // Print NULL    cout << y.get() << endl; // Print non-NULL address i

}

This code will print a NULL address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=). The raw pointer i in the example should not be deleted, as it will be deleted by the auto_ptr that owns the reference. In fact, new int could be passed directly into x, eliminating the need for i.

Notice that the object pointed by an auto_ptr is destroyed using operator delete; this means that you should only use auto_ptr for pointers obtained with operator new. This excludes pointers returned by malloc/calloc/realloc, and pointers to arrays (because arrays are allocated by operator new[] and must be deallocated by operator delete[]).

Because of its copy semantics, auto_ptr may not be used in STL containers that may perform element copies in their operations.

See also

  • Smart pointer
  • Generic programming

References

1. ^{{cite web |url=http://msdn2.microsoft.com/en-us/library/ew3fk483.aspx |publisher=Microsoft |title=auto_ptr Class |accessdate=2006-09-27}}
2. ^{{cite web|title=Working Draft, Standard for Programming Language C++ N3242|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf|accessdate=2013-02-17|page=1233|date=28 February 2011}}
3. ^{{cite web|last=Kalev|first=Danny|title=Using unique_ptr, Part I|url=http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400|publisher=informIT|accessdate=30 September 2010}}
4. ^{{cite web|title=Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm|accessdate=2016-08-29|date=2014-10-09}}
5. ^{{cite web |url=http://www.ddj.com/dept/cpp/184401839 |publisher=Dr. Dobb's |title=Collecting Shared Objects |date=2004-07-01 |accessdate=2006-09-27}}

External links

  • Using auto_ptr effectively
  • Avoiding Memory Leaks with auto_ptr
  • Article "[https://web.archive.org/web/20080306193914/http://gethelp.devx.com/techtips/cpp_pro/10min/10min1199.asp Using the auto_ptr Class Template to Facilitate Dynamic Memory Management]" by Danny Kalev
  • Article "Container of auto_ptr" by Zeeshan Amjad
  • Article "Update on auto_ptr" by Scott Meyers
  • [https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.0/classstd_1_1auto__ptr.html auto_ptr Class Template Reference from GNU libstdc++]
  • auto_ptr reference from Rogue Wave

3 : Articles with example C++ code|C++ Standard Library|Articles with underscores in the title

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/13 0:43:42