How does returning std::make_unique work?

后端 未结 5 2376
旧巷少年郎
旧巷少年郎 2021-02-19 16:05

I have a base class and its subclass:

class Base {
    public:
    virtual void hi() {
        cout << \"hi\" << endl;
    } 
};

class Derived : pub         


        
5条回答
  •  伪装坚强ぢ
    2021-02-19 17:02

    You will find that std::unique_ptr can move from derived's to base's, if you look into its definition. Basically is_convertible here check this situation.

       /** @brief Converting constructor from another type
       *
       * Requires that the pointer owned by @p __u is convertible to the
       * type of pointer owned by this object, @p __u does not own an array,
       * and @p __u has a compatible deleter type.
       */
      template,
           typename conditional::value,
                    is_same<_Ep, _Dp>,
                    is_convertible<_Ep, _Dp>>::type>>
    unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
    : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
    { }
    

提交回复
热议问题