Why was auto_ptr removed?
auto_ptr was depreciated in C++ 11 and removed in C++ 17. The removal of the auto_ptr was due to the following limitations:
- An auto_ptr can’t be used to point to an array. While deleting the pointer to an array, we need to use delete[] but in auto_ptr we can only use delete.
- An auto_ptr can not be used with STL Containers because the containers, or algorithms manipulating them, might copy the stored elements. Copies of auto_ptrs aren’t equal because the original is set to NULL after being copied.
- The auto_ptr does not fit in the move semantics as they implement move by using copy operation.
Due to the above limitations, auto_ptr was removed from C++ and later replaced with unique_ptr.
auto_ptr in C++
In C++, a memory leak may occur while de-allocating a pointer. So to ensure that the code is safe from memory leaks and exceptions, a special category of pointers was introduced in C++ which is known as Smart Pointers. In this article, we will discuss the auto pointer(auto_ptr) which is one of the smart pointers in C++.
Pre-Requisite: Pointer in C++, Smart Pointers in C++
Note: Auto Pointer was deprecared in C++11 and removed in C++17