2016-06-13 4 views
4

auto_ptrにテンプレートコピーコンストラクタとオーバーライド演算子関数があるのはなぜですか?auto_ptrにテンプレートコピーコンストラクタとオーバーライド演算子関数があるのはなぜですか?

C++のISO標準では、auto_ptrに次のインターフェイスを指定しています。 (これは、2003標準のまっすぐコピーされます。)

namespace std { 
    template <class Y> struct auto_ptr_ref {}; 

    template<class X> class auto_ptr { 
    public: 
     typedef X element_type; 

     // 20.4.5.1 construct/copy/destroy: 
     explicit auto_ptr(X* p =0) throw(); 
     auto_ptr(auto_ptr&) throw(); 
     template<class Y> auto_ptr(auto_ptr<Y>&) throw(); 
     auto_ptr& operator=(auto_ptr&) throw(); 
     template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw(); 
     auto_ptr& operator=(auto_ptr_ref<X> r) throw(); 
     ~auto_ptr() throw(); 

     // 20.4.5.2 members: 
     X& operator*() const throw(); 
     X* operator->() const throw(); 
     X* get() const throw(); 
     X* release() throw(); 
     void reset(X* p =0) throw(); 

     // 20.4.5.3 conversions: 
     auto_ptr(auto_ptr_ref<X>) throw(); 
     template<class Y> operator auto_ptr_ref<Y>() throw(); 
     template<class Y> operator auto_ptr<Y>() throw(); 
    }; 

がなぜあります

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

私はちょうどauto_ptr(auto_ptr&) throw();OKだと思います。

+0

明らかに、これらのメソッドは(おそらく)異なるテンプレートタイプで動作します。あなたのバージョンは同じもののみです。 – deviantfan

答えて

7

auto_ptrBaseのクラスタイプをDerivedで初期化できます。それがなければauto_ptr<Base>auto_ptr<Derived>は全く関係のないタイプです。

struct Base {}; 
struct Derived : Base {}; 

auto_ptr<Derived> d(new Derived); 
auto_ptr<Base> b = d; 
関連する問題