template< class Y >
explicit shared_ptr( Y* ptr );
其中T可以等於Y,也可以不等於Y。
那麼對於它的拷貝構造函數:
shared_ptr( const shared_ptr& r ) noexcept;
和
template< class Y >
shared_ptr( const shared_ptr<Y>& r ) noexcept;
當T等於Y時就是
shared_ptr( const shared_ptr& r ) noexcept;
當T不等於Y時就是
template< class Y >
shared_ptr( const shared_ptr<Y>& r ) noexcept;
那麼我的疑問是,既然用指針構造時T可以等於Y,也可以不等於Y,那麼
template< class Y >
shared_ptr( const shared_ptr<Y>& r ) noexcept;
也應該包含了這兩種情況,爲何會多此一舉定義一個shared_ptr( const shared_ptr& r ) noexcept;
呢?
原來模板拷貝構造函數不屬於構造函數,就算我們只定義了
template< class Y >
shared_ptr( const shared_ptr<Y>& r ) noexcept;
編譯器也會默認合成一個
shared_ptr( const shared_ptr& r ) noexcept;
所以不管定沒定義模板拷貝構造函數,shared_ptr( const shared_ptr& r ) noexcept;
總是存在的。