samedi 5 juin 2021

What is the need of templated special member function and assignment operator function?

Let's say we have std unique_ptr

constexpr unique_ptr() noexcept;
constexpr unique_ptr( std::nullptr_t ) noexcept;
(1) 
explicit unique_ptr( pointer p ) noexcept;
(2) 
unique_ptr( pointer p, /* see below */ d1 ) noexcept;
(3) 
unique_ptr( pointer p, /* see below */ d2 ) noexcept;
(4) 
unique_ptr( unique_ptr&& u ) noexcept;
(5) 
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u ) noexcept; (6)

Example taken from cppreference.com Why we need the templated constructor in case 6?

Same goes for operator functions

unique_ptr& operator=( unique_ptr&& r ) noexcept;
(1) 
template< class U, class E >
unique_ptr& operator=( unique_ptr<U,E>&& r ) noexcept;
(2) 
unique_ptr& operator=( std::nullptr_t ) noexcept;
(3)

In the above, the 2nd function. I have seen this in pair also. Is there any purpose?

Aucun commentaire:

Enregistrer un commentaire