我的編譯器不支持make_unique。怎么寫一個?template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args );
3 回答

小怪獸愛吃肉
TA貢獻1852條經驗 獲得超1個贊
復制自make_unique和完美轉發(Herb Sutter的博客中提供了同樣的內容)
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
如果在VC2012中需要它,請參閱是否可以在VS2012 中編寫make_unique()?
不過,如果sasha.sochka答案中的解決方案可以使用您的編譯器進行編譯,那么我會選擇該解決方案。這更加復雜,并且也適用于數組。

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
的目的是,正確使用該_Known_bound
版本時,永遠不要匹配該版本make_unique
。如果有人嘗試使用它,因為make_unique<T[N]>()
它將與_Known_bound
版本匹配并導致錯誤。
- 3 回答
- 0 關注
- 777 瀏覽
添加回答
舉報
0/150
提交
取消