COMMENTS

  1. std::unique_ptr

    std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the pImpl idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr.

  2. c++

    It may be misleading in cases where the unique_ptr was previously empty. Moreover reset() solution has the benefit of being shorter, more explicit (you can see the "new") using assignment form make_unique conveys the meaning on a more abstract level: "make an unique someObject instance and place it here". Some people find that clearer and more ...

  3. unique_ptr

    The assignment operation between unique_ptr objects that point to different types (3) needs to be between types whose pointers are implicitly convertible, and shall not involve arrays in any case (the third signature is not part of the array specialization of unique_ptr). Copy assignment (4) to a unique_ptr type is not allowed (deleted signature).

  4. 22.5

    std::unique_ptr is by far the most used smart pointer class, so we'll cover that one first. In the following lessons, we'll cover std::shared_ptr and std::weak_ptr. std::unique_ptr. std::unique_ptr is the C++11 replacement for std::auto_ptr. It should be used to manage any dynamically allocated object that is not shared by multiple objects.

  5. Unique_ptr in C++

    std::unique_ptr is a smart pointer introduced in C++11. It automatically manages the dynamically allocated resources on the heap. Smart pointers are just wrappers around regular old pointers that help you prevent widespread bugs. Namely, forgetting to delete a pointer and causing a memory leak or accidentally deleting a pointer twice or in the ...

  6. unique_ptr

    a stored deleter: a callable object that takes an argument of the same type as the stored pointer and is called to delete the managed object. It is set on construction, can be altered by an assignment operation, and can be individually accessed using member get_deleter. unique_ptr objects replicate a limited pointer functionality by providing ...

  7. std::unique_ptr<T,Deleter>:: operator=

    For the array specialization (unique_ptr<T[]>), this overload participates in overload resolution only if U is an array type, ... As a move-only type, unique_ptr's assignment operator only accepts rvalues arguments (e.g. the result of std::make_unique or a std::move 'd unique_ptr variable).

  8. libstdc++: std::unique_ptr< _Tp, _Dp > Class Template Reference

    unique_ptr (const unique_ptr &)=delete ... Assignment from another type. Parameters. __u: The object to transfer ownership from, which owns a convertible pointer to a non-array object. Invokes the deleter if this object owns a pointer. Definition at line 424 of file unique_ptr.h.

  9. std::unique_ptr<T,Deleter>:: unique_ptr

    unique_ptr(const unique_ptr&)= delete; (7) 1) Constructs a std::unique_ptr that owns nothing. Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception. These overloads participate in overload resolution only if std::is_default_constructible ...

  10. std::unique_ptr

    std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. ... T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr.

  11. std::unique_ptr

    std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the Pimpl idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr.

  12. unique_ptr

    Constructs a unique_ptr object, depending on the signature used: default constructor (1), and (2) The object is empty (owns nothing), with value-initialized stored pointer and stored deleter. construct from pointer (3) The object takes ownership of p, initializing its stored pointer to p and value-initializing its stored deleter. construct from pointer + lvalue deleter (4)

  13. Understanding unique_ptr with Example in C++11

    std::auto_ptr, and why to avoid it#. What we have seen above as smart_ptr is basically an std::auto_ptr which was introduced in C++98, was C++'s first attempt at a standardized smart pointer.; However, std::auto_ptr (and our smart_ptr class) has a number of problems that make using it dangerous. Because std::auto_ptr implements move semantics through the copy constructor and assignment ...

  14. How to: Create and use unique_ptr instances

    In this article. A unique_ptr does not share its pointer. It cannot be copied to another unique_ptr, passed by value to a function, or used in any C++ Standard Library algorithm that requires copies to be made.A unique_ptr can only be moved. This means that the ownership of the memory resource is transferred to another unique_ptr and the original unique_ptr no longer owns it.

  15. unique_ptr Class

    unique_ptr uniquely manages a resource. Each unique_ptr object stores a pointer to the object that it owns or stores a null pointer. A resource can be owned by no more than one unique_ptr object; when a unique_ptr object that owns a particular resource is destroyed, the resource is freed. A unique_ptr object may be moved, but not copied; for ...

  16. std::unique_ptr<T,Deleter>:: operator*, std::unique_ptr<T ...

    pointer operator->()constnoexcept; (2) (since C++11)(constexpr since C++23) operator* and operator-> provide access to the object owned by *this . The behavior is undefined if get()== nullptr . These member functions are only provided for unique_ptr for the single objects i.e. the primary template.

  17. std::unique_ptr<T,Deleter>:: reset

    Return value (none) [] NoteTo replace the managed object while supplying a new deleter as well, move assignment operator may be used. A test for self-reset, i.e. whether ptr points to an object already managed by * this, is not performed, except where provided as a compiler extension or as a debugging assert.Note that code such as p. reset (p. release ()) does not involve self-reset, only code ...

  18. My implementation for std::unique_ptr

    Copy assignment operator unique_ptr<T>& operator=(const unique_ptr<T>& uptr) = delete; This is redundant, this part is not necessary as this operator will never be called taking into account that the motion constructor exists and the copy constructor is disabled (= delete).

  19. Is it safe to assign a unique_ptr to another unique_ptr inside the

    Only the move assignment operator= of the target unique_ptr will set the source unique_ptr to empty state. Citing from the reference for unique_ptr move assignment, "Transfers ownership from r to *this as if by calling reset(r.release()) ", so source unique_ptr will be released first before target will be reset (). Still looks fine in my opinion.

  20. What happens when redirecting unique_ptr?

    You pass this pointer to a new unique_ptr using unique_ptr<int>(new int(2)), this is a temporary instance of unique_ptr (we will see why in a second) and it stores the pointer to the second int. You assign the temporary object to p. Now the assignment operator is defined to delete the previously owned object (the first int) and take ownership ...

  21. Copy constructor for a class with unique_ptr

    There are basically two things going on here: The first is the addition of a user-defined copy constructor of Foo, This is necessary, as the unique_ptr -member iself has no copy constructor. In the declared copy-constructor, a new unique_ptr is created, and the pointer is set to a copy of the original pointee.