Is unique_ptr to unique_ptr<Base> up-casting automatic?

前端 未结 1 1475
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 22:17

I know it is possible that a derived class unique_ptr can take place where base class unique_ptr is required for polymorphic types. For example, wh

1条回答
  •  無奈伤痛
    2020-12-18 23:04

    The (draft) standard says:

    // 20.8.1.2.1, constructors
    ...
    template 
      unique_ptr(unique_ptr&& u) noexcept;
    template 
      unique_ptr(auto_ptr&& u) noexcept;
    

    Those are constructors from any unique_ptr. The standard further restricts their usage by clauses like this:

    24 Remarks: This constructor shall not participate in overload resolution unless U* is implicitly convertible to T* and D is the same type as default_delete

    The effect of this remark is that unique_ptr is constructible from unique_ptr precisely U* is convertible to T* (and all deleter requirements are met). In particular, when T is an unambiguous public base class of U.

    Since the constructor is not explicit, it serves as an implicit converter from unique_ptr to unique_ptr.

    0 讨论(0)
提交回复
热议问题