STL中有一些以make_
前綴開頭的函數,如std::make_pair
,std::make_shared
,std::make_unique
等。爲什麼使用它們而不是簡單地使用構造函數是一種更好的做法?爲什麼使用std :: make_ *而不是構造函數更好?
auto pair2 = std::pair< int, double >(1, 2.0);
auto pair3 = std::make_pair(1, 2.0);
std::shared_ptr<int> pointer1 = std::shared_ptr<int>(new int(10));
std::shared_ptr<int> pointer2 = std::make_shared<int>(10);
- 我剛纔看到的是,這些功能使代碼更短一點,但是是這樣嗎?
- 還有其他優點嗎?
- 這些功能是否安全使用?
不太確定新的智能指針,但看看http://stackoverflow.com/questions/9270563/purpose-of-stdmake-pair對。 –
我相信每個人都有明顯的優勢,在不同的現有問題中回答。 'make_pair' [作品沒有作者知道類型](http://stackoverflow.com/questions/9270563)。 'make_shared' [提供異常安全性](http://stackoverflow.com/questions/20895648)。 –
更好,因爲你輸入較少? – texasbruce