作者littleshan (我要加入劍道社!)
看板C_and_CPP
標題Re: [問題] auto_ptr_ref的功用
時間Thu Apr 30 12:22:04 2009
※ 引述《firose (guest也是也是也是也是也)》之銘言:
: ※ 引述《omomo34415 ( )》之銘言:
: : 請問auto_ptr的實作中為什麼要多定義
: : auto_ptr(auto_ptr_ref<T> rhs) throw() : ap(rhs.yp) {
: : }
: : auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new
: : reset(rhs.yp);
: : return *this;
: : }
: : template<class Y> operator auto_ptr_ref<Y>() throw() {
: : return auto_ptr_ref<Y>(release());
: : }
: : template<class Y> operator auto_ptr<Y>() throw() {
: : return auto_ptr<Y>(release());
: : }
: : 其他的CODE的在http://www.josuttis.com/libbook/util/autoptr.hpp.html
: : 看不太懂這些出現的時機。
: 因為 auto_ptr 的 ctor 是 explicit 的, 導致不能把一個 pointer 設給 auto_ptr
: 所以他才用一個 aotu_ptr_ref 讓指標可以做一次 user-define conversion 然後再
: 經由接受 auto_ptr_ref 的 auto_ptr constructor 讓這個行為可以 work.
不是的,auto_ptr_ref 並不是用在這種地方。
想像以下的程式碼:
auto_ptr<int> foo();
void bar()
{
...
auto_ptr<int> p_base = foo();
}
foo() 回傳的是一個暫時物件,但 auto_ptr 的 copy constructor 接受的卻是
non-const reference,這樣會造成編譯錯誤,因為暫時物件無法以 non-const
reference 的型式傳遞。解決之道是先把 auto_ptr 隱式轉型成 auto_ptr_ref,
然後讓 auto_ptr 的 constructor 可以接受 auto_ptr_ref 作為參數。
auto_ptr_ref 應該只用來做這類的轉換,而不應該拿來轉換一般的 pointer。
像以下的 code:
auto_ptr<int> p = new int(10);
在 VC 上,右邊的 int* 會被隱式轉換成 auto_ptr_ref,然後再送到 auto_ptr
的 constructor 中。這並不是正確的行為,VC 應該把 auto_ptr_ref 的
constructor 宣告為 explicit。
: 問題是 auto_ptr 為什麼要 explicit ? 因為如果他不這樣做的話, 然仍不能把指標
: 設給 auto_ptr , 因為它的 copy constructor/assignment 是接受 non-const 參考
這樣說就不對了
當你有一個 constructor 接受指標當作參數時
直接用指標去初始化並不會呼叫 copy constructor 也不會呼叫 copy assignement
template <typename T>
class auto_ptr {
public:
auto_ptr(T* p) {...} // 沒有 explicit
auto_ptr(auto_ptr<T>& p) {...} // copy constructor
};
auto_ptr<int> p = new int(10);
上述的程式碼會合法呼叫 auto_ptr<int>::auto_ptr<int>(int*),而不會去呼叫
copy constructor。
: 會這樣設計是因為它的內容呼叫 auto_ptr::release 這個函式會把內含指標 ap 設成 0
: 使 auto_ptr 可以順利把指標的擁有權轉移, 不會在 ~auto_ptr 刪除他, 所以 release
: 會是 non-const. 如果 copy constructor/assignment 接受 const reference 的話
: 是不能呼叫 non-const release 的, 所以它在這個設計裡必須是 non-const
auto_ptr 的 constructor 必需宣告為 explicit,否則當指標被隱式轉型成
auto_ptr 時,使用者會發現他在預期外的地方失去了指標的所有權:
void foo(auto_ptr<int> p); // foo 結束時,p 會自動被釋放
void bar()
{
int* p = new int(10);
foo(p); // 失去了 p 的所有權,但使用者一無所知!
delete p; // 重覆釋放 p,悲劇一場
}
使用 explicit constructor 可以讓使用者清楚地知道自己做了什麼事:
void bar()
{
int* p = new int(10);
foo( auto_ptr<int>(p) ); // 把 p 轉為 auto_ptr,我們已失去所有權
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.87.151.2
推 firose:嗯, 你說的對, 你的第二段地部分我忽略了導致錯誤的想法 04/30 12:29
→ firose:第三段才是真正的原因~ 小弟對不起原 PO ... sorry = = 04/30 12:30
→ firose:所以說 auto_ptr_ref() 應該要 explicit 不然一樣不太正確 04/30 12:31
推 firose:比較好奇的是 auto_ptr_ref 如果 explicit 那為何需要他? 04/30 12:51
→ littleshan:我的第一段就是解釋為何需要 auto_ptr_ref 04/30 13:02
→ littleshan:因為要讓 function 回傳的暫時物件能夠 assign 04/30 13:03
推 firose:您指的暫時物件是 auto_ptr 還是 auto_ptr_ref? 04/30 13:04
→ firose:我的意思是如果兩個都 explicit 那不如建構暫時 auto_ptr 04/30 13:11
→ firose:如果他本來就 auto_ptr 我也不需要用轉型運算子轉成 04/30 13:12
→ firose:auto_ptr_ref 直接遞交 ownership 就好了? 04/30 13:13
→ firose:除非當時因為某種原因 foo 已經明確建構出 auto_ptr_ref 04/30 13:14
→ firose:不然似乎很奇怪, 但若 auto_ptr_ref 不 explicit 似乎仍然 04/30 13:15
→ firose:會有第三段所說的問題? 04/30 13:16
推 VictorTom:看完諸位強者的討論, 小弟我認真的覺得自己不懂程式Orz. 04/30 13:20
→ firose:抱歉! 我想應該是在 foo 建構 auto_ptr_ref<T>(pointer) 04/30 13:22
→ firose:然後回傳的時候直接呼叫 auto_ptr_ref 版的 auto_ptr建構式 04/30 13:23
→ firose:因為直接建構 auto_ptr 不能拷貝, 他用 auto_ptr_ref 提供 04/30 13:24
→ firose:一個拷貝的途徑, 但是他應該 explicit 使用者也應該明確建 04/30 13:25
→ firose:這樣的意思應該是說在拷貝前不需建構 temp auto_ptr 因為編 04/30 13:27
→ firose:編譯器會發現這不必要, 所以會 optimize 掉! 04/30 13:28