auto_ptr was deprecated from gcc 4.7.1 and above or when compiled with --std=c++11 will start to give warning, and is completely removed from --std=c++17.
The main reason is, it moves from lvalue via copy/assignment operator.
For Eg:
#include <iostream>
#include <memory>
int main()
{
auto printNull = [] (std::auto_ptr<int> &value, std::string name)
{
(value.get() == nullptr) ? std::cout << name << " is nullptr\n"
: std::cout << name << " is not nullptr, & contains " << *value << "\n";
};
std::auto_ptr<int> p_int(new int{5});
printNull(p_int, "p_int");
std::auto_ptr<int> p_new_int;
p_new_int = p_int; /*Moves instead of copy*/
printNull(p_new_int, "p_new_int");
printNull(p_int, "p_int");
return 0;
}
****OUTPUT****
p_int is not nullptr, & contains 5
p_new_int is not nullptr, & contains 5
p_int is nullptr
So it's definitely not a good thing, and it was not aligning itself with modern cpp ideologies.
In most cases auto_ptr can be directly swapped with std::unique_ptr<>, but in larger systems caution is advised before changing.
Even thought it is removed from c++17, I can still compile it in gcc 13.1 link this was kind of strange.
Top comments (0)