Why the interface for auto_ptr defines two constructor type constructors

I was looking at the auto_ptr documentation at this link auto_ptr There is something that I could not fully understand why this is done. There are two declarations in the interface section for its copy constructor

1)

auto_ptr(auto_ptr<X>&) throw (); 

2)

template <class Y> 
     auto_ptr(auto_ptr<Y>&) throw(); 

What is it for?

+3
source share
2 answers

This is where you can implicitly convert pointers:

struct base {};
struct derived : base {};

std::auto_ptr<derived> d(new derived);
std::auto_ptr<base> b(d); // converts 

, , , - const. , auto_ptr . , b, d . auto_ptr , .

++ 0x auto_ptr unique_ptr. , - -. , , "" :

std::unique_ptr<derived> d(new derived);

std::unique_ptr<base> b(d); // nope, cannot be copied
std::unique_ptr<base> b(std::move(d)); // but can be moved

unique_ptr , , .

+5

- , - auto_ptr .

, , . , , , , , - , .

+5

Source: https://habr.com/ru/post/1729651/


All Articles