我必須創建一個函數來爲我創建一組字符。我不確定我應該選擇以下哪種方法。 據我瞭解,我不應該使用createSet1,因爲如果在返回s之前出現錯誤,它將會泄漏。C++返回指針與返回std :: move的本地對象
set<char>* createSet1(){
set<char>* s = new set<char>;
//does something
return s;
}
set<char> createSet2(){
set<char> s;
//does something
return std::move(s);
}
unique_ptr<set<char>> createSet3(){
unique_ptr<set<char>> s(new set<char>);
//does something
return s;
}
我會很高興,如果有人能解釋我應該更喜歡哪一個,爲什麼。
沒有必要「說」'返回標準: :move(...)',因爲返回的值是一個右值。你真的明白了'unique_ptr'的情況(你注意到'unique_ptr'是隻移動嗎?) – sehe
啊,好吧,我認爲std :: move是不必要的,如果它不是那些「聰明」指針(unique_ptr ,shared_ptr)。乾杯 –