簡體   English   中英

std::unique_ptr::reset 重載問題

[英]std::unique_ptr::reset overloads questions

來自主模板的https://en.cppreference.com/w/cpp/memory/unique_ptr/reset成員,unique_ptr

void reset( pointer ptr = pointer() ) noexcept;     (1)     

        
template< class U >
void reset( U ) noexcept;       (2)     
void reset( std::nullptr_t p = nullptr ) noexcept;      (3)     

對我來說,對於(1)來說,如果沒有參數是給定的,那么將調用指針類型的默認構造函數。 但是它應該表現得像一個nullptr,這樣unique_ptr里面的指針就會被刪除,它會被設置為null,怎么會呢?

(2) 的解釋是

2) Behaves the same as the reset member of the primary template, except that it will only participate in overload resolution if either:    
    U is the same type as pointer, or
    pointer is the same type as element_type* and U is a pointer type V* such that V(*)[] is convertible to element_type(*)[].

我真的無法理解,有人可以解釋/改寫嗎?

對我來說,對於(1)來說,如果沒有參數是給定的,那么將調用指針類型的默認構造函數。 但是它應該表現得像一個nullptr,這樣unique_ptr里面的指針就會被刪除,它會被設置為null,怎么會呢?

指針類型的默認構造函數”並不是一個真正的東西 - 但是,是的,默認參數是一個零初始化的T* ,它與你所追求的效果相同。 pointer() (其中pointerT*typedef )將被初始化為nullptr

using pointer = foo*;    // example pointer typedef
pointer ptr = pointer(); // initialized to nullptr by default

這相當於nullptr並且持有的任何資源都將被釋放。

重載 (2) 和 (3) 用於數組特化,其中U = T[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM