##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Standard library header <memory>

From cppreference.com
< cpp‎ | header

This header is part of the dynamic memory management library.

### [edit] Classes

##### Smart pointers categories
unique_ptr (C++11) | smart pointer with unique object ownership semantics (class template) [edit]
shared_ptr (C++11) | smart pointer with shared object ownership semantics (class template) [edit]
weak_ptr (C++11) | weak reference to an object managed by std::shared_ptr (class template) [edit]
auto_ptr (deprecated) | smart pointer with strict object ownership semantics (class template) [edit]
##### Smart pointers helper classes
owner_less (C++11) | provides mixed-type owner-based ordering of shared and weak pointers (class template) [edit]
enable_shared_from_this (C++11) | allows an object to create a `shared_ptr` referring to itself (class template) [edit]
bad_weak_ptr (C++11) | exception thrown when accessing a `weak_ptr` which refers to already destroyed object (class) [edit]
default_delete (C++11) | default deleter for `unique_ptr` (class template) [edit]
##### Allocators
allocator | the default allocator (class template) [edit]
allocator_traits (C++11) | provides information about allocator types (class template) [edit]
allocator_arg_t (C++11) | tag type used to select allocator-aware constructor overloads (class) [edit]
allocator_arg (C++11) | an object of type std::allocator_arg_t used to select allocator-aware constructors (constant) [edit]
uses_allocator (C++11) | checks if the specified type supports uses-allocator construction (class template) [edit]
##### Other
pointer_safety (C++11) | lists pointer safety models (class) [edit]
pointer_traits (C++11) | provides information about pointer-like types (class template) [edit]
std::hash<std::shared_ptr> (C++11) | hash support for std::shared_ptr (class template specialization) [edit]
std::hash<std::unique_ptr> (C++11) | hash support for std::unique_ptr (class template specialization) [edit]
##### Forward declaration
Defined in header `<functional>`
hash (C++11) | hash function object (class template) [edit]
### Constants
allocator_arg (C++11) | an object of type std::allocator_arg_t used to select allocator-aware constructors (constant) [edit]

### [edit] Functions

##### Uninitialized storage
uninitialized_copy | copies a range of objects to an uninitialized area of memory (function template) [edit]
uninitialized_copy_n (C++11) | copies a number of objects to an uninitialized area of memory (function template) [edit]
uninitialized_fill | copies an object to an uninitialized area of memory (function template) [edit]
uninitialized_fill_n | copies an object to an uninitialized area of memory (function template) [edit]
raw_storage_iterator | an iterator that allows standard algorithms to store results in uninitialized memory (class template) [edit]
get_temporary_buffer | obtains uninitialized storage (function template) [edit]
return_temporary_buffer | frees uninitialized storage (function template) [edit]
##### Garbage collector support
declare_reachable (C++11) | declares that an object can not be recycled (function) [edit]
undeclare_reachable (C++11) | declares that an object can be recycled (function template) [edit]
declare_no_pointers (C++11) | declares that a memory area does not contain traceable pointers (function) [edit]
undeclare_no_pointers (C++11) | cancels the effect of std::declare_no_pointers (function) [edit]
get_pointer_safety (C++11) | returns the current pointer safety model (function) [edit]
##### Miscellaneous
addressof (C++11) | obtains actual address of an object, even if the & operator is overloaded (function template) [edit]
align (C++11) | aligns a pointer in a buffer (function) [edit]
##### Smart pointer non-member operations
make_shared | creates a shared pointer that manages a new object (function template) [edit]
allocate_shared | creates a shared pointer that manages a new object allocated using an allocator (function template) [edit]
static_pointer_cast dynamic_pointer_cast const_pointer_cast | applies `static_cast`, `dynamic_cast` or `const_cast` to the type of the managed object (function template) [edit]
get_deleter | returns the deleter of specified type, if owned (function template) [edit]
operator==operator!=operator<operator<=operator>operator>= | compares with another `shared_ptr` or with nullptr (function template) [edit]
operator<< | outputs the value of the managed pointer to an output stream (function template) [edit]
std::swap(std::shared_ptr) (C++11) | specializes the std::swap algorithm (function template) [edit]

std::atomic_is_lock_free(std::shared_ptr)std::atomic_load(std::shared_ptr)std::atomic_load_explicit(std::shared_ptr)std::atomic_store(std::shared_ptr)std::atomic_store_explicit(std::shared_ptr)std::atomic_exchange(std::shared_ptr)std::atomic_exchange_explicit(std::shared_ptr)std::atomic_compare_exchange_weak(std::shared_ptr)std::atomic_compare_exchange_strong(std::shared_ptr)std::atomic_compare_exchange_weak_explicit(std::shared_ptr)std::atomic_compare_exchange_strong_explicit(std::shared_ptr) | specializes atomic operations (function template) [edit]

operator==operator!=operator<operator<=operator>operator>= | compares to another `unique_ptr` or with nullptr (function template) [edit]
std::swap(std::unique_ptr) (C++11) | specializes the std::swap algorithm (function template) [edit]
std::swap(std::weak_ptr) (C++11) | specializes the std::swap algorithm (function template) [edit]
##### Allocator non-member operations
operator==operator!= | compares two allocator instances (public member function of `std::allocator`) [edit]
operator==operator!= | compares two scoped_allocator_adaptor instances (public member function of `std::scoped_allocator_adaptor`) [edit]

### [edit] Synopsis

    namespace std {
        // pointer traits
        template <class Ptr> struct pointer_traits;
        template <class T> struct pointer_traits<T*>;

        // pointer safety
        enum class pointer_safety { relaxed, preferred, strict };
        void declare_reachable(void *p);
        template <class T> T *undeclare_reachable(T *p);
        void declare_no_pointers(char *p, size_t n);
        void undeclare_no_pointers(char *p, size_t n);
        pointer_safety get_pointer_safety() noexcept;

        // pointer alignment function
        void *align(std::size_t alignment, std::size_t size,
                    void *&ptr, std::size_t& space);

        // allocator argument tag
        struct allocator_arg_t { };
        constexpr allocator_arg_t allocator_arg = allocator_arg_t();

        // uses_allocator
        template <class T, class Alloc> struct uses_allocator;

        // allocator traits
        template <class Alloc> struct allocator_traits;

        // the default allocator:
        template <class T> class allocator;
        template <> class allocator<void>;
        template <class T, class U>
            bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
        template <class T, class U>
            bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;

        // raw storage iterator:
        template <class OutputIterator, class T> class raw_storage_iterator;

        // temporary buffers:
        template <class T>
            pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
        template <class T>
            void return_temporary_buffer(T* p);

        // specialized algorithms:
        template <class T> T* addressof(T& r) noexcept;
        template <class InputIterator, class ForwardIterator>
            ForwardIterator uninitialized_copy(InputIterator first, InputIterator last,
                                               ForwardIterator result);
        template <class InputIterator, class Size, class ForwardIterator>
            ForwardIterator uninitialized_copy_n(InputIterator first, Size n,
                                                 ForwardIterator result);
        template <class ForwardIterator, class T>
            void uninitialized_fill(ForwardIterator first, ForwardIterator last,
                                    const T& x);
        template <class ForwardIterator, class Size, class T>
            ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
        // class template unique_ptr:
        template <class T> class default_delete;
        template <class T> class default_delete<T[]>;
        template <class T, class D = default_delete<T>> class unique_ptr;
        template <class T, class D> class unique_ptr<T[], D>;

        template <class T1, class D1, class T2, class D2>
            bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
        template <class T1, class D1, class T2, class D2>
            bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
        template <class T1, class D1, class T2, class D2>
            bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
        template <class T1, class D1, class T2, class D2>
            bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
        template <class T1, class D1, class T2, class D2>
            bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
        template <class T1, class D1, class T2, class D2>
            bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);

        template <class T, class D>
            bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
        template <class T, class D>
            bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
        template <class T, class D>
            bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
        template <class T, class D>
            bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;

        template <class T, class D>
            bool operator<(const unique_ptr<T, D>& x, nullptr_t);
        template <class T, class D>
            bool operator<(nullptr_t, const unique_ptr<T, D>& y);
        template <class T, class D>
            bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
        template <class T, class D>
            bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
        template <class T, class D>
            bool operator>(const unique_ptr<T, D>& x, nullptr_t);
        template <class T, class D>
            bool operator>(nullptr_t, const unique_ptr<T, D>& y);
        template <class T, class D>
            bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
        template <class T, class D>
            bool operator>=(nullptr_t, const unique_ptr<T, D>& y);

        // class bad_weak_ptr:
        class bad_weak_ptr;

        // class template shared_ptr:
        template<class T> class shared_ptr;

        // shared_ptr comparisons:
        template<class T, class U>
            bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
        template<class T, class U>
            bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
        template<class T, class U>
            bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
        template<class T, class U>
            bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
        template<class T, class U>
            bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
        template<class T, class U>
            bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;

        template <class T>
            bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
            bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
        template <class T>
            bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
            bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
        template <class T>
            bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
            bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
        template <class T>
            bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
            bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
        template <class T>
            bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
        bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
        template <class T>
            bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
        template <class T>
            bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;

        // shared_ptr specialized algorithms:
        template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;

        // shared_ptr casts:
        template<class T, class U>
            shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
        template<class T, class U>
            shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
        template<class T, class U>
            shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;

        // shared_ptr get_deleter:
        template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;

        // shared_ptr I/O:
        template<class E, class T, class Y>
            basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);

        // class template weak_ptr:
        template<class T> class weak_ptr;

        // weak_ptr specialized algorithms:
        template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;

        // class template owner_less:
        template<class T> class owner_less;

        // class template enable_shared_from_this:
        template<class T> class enable_shared_from_this;

        // shared_ptr atomic access:
        template<class T>
            bool atomic_is_lock_free(const shared_ptr<T>* p);
        template<class T>
            shared_ptr<T> atomic_load(const shared_ptr<T>* p);
        template<class T>
            shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
        template<class T>
            void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
        template<class T>
            void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
        template<class T>
            shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
        template<class T>
            shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r,
                                                    memory_order mo);
        template<class T>
            bool atomic_compare_exchange_weak(
                shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T>  w);
        template<class T>
            bool atomic_compare_exchange_strong(
                shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T>  w);
        template<class T>
            bool atomic_compare_exchange_weak_explicit(
                shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
                memory_order success, memory_order failure);
        template<class T>
            bool atomic_compare_exchange_strong_explicit(
                shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w,
                memory_order success, memory_order failure);

        //  hash support
        template <class T> struct hash;
        template <class T, class D> struct hash<unique_ptr<T, D> >;
        template <class T> struct hash<shared_ptr<T> >;

        // auto_ptr (deprecated)
        template <class X> class auto_ptr;
    }

| This section is incomplete Reason: Class synopses

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/header/memory&oldid=57511"
Category:

- Todo with reason