如何在C ++ 11中实现make_unique函数? [英] How to implement make_unique function in C++11?
本文介绍了如何在C ++ 11中实现make_unique函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的编译器不支持make_unique。如何写一个?
My compiler doesn't support make_unique. How to write one?
template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args );
推荐答案
从 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的回答中的解决方案与您的编译器一起编译,我会使用那个编译器。这是更复杂的,也适用于数组。
Nevertheless, if the solution in sasha.sochka's answer compiles with your compiler, I would go with that one. That is more elaborate and works with arrays as well.
这篇关于如何在C ++ 11中实现make_unique函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文