我的编译器不支持 make_unique。一个怎么写?
template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args );
最佳答案
复制自 make_unique and perfect forwarding (Herb Sutter's blog 中同样给出)
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 中需要它,请参阅 Is there a way to write make_unique() in VS2012?
不过,如果 sasha.sochka's answer 中的解决方案用你的编译器编译,我会用那个。这更精细,也适用于数组。
关于c++ - 如何在 C++11 中实现 make_unique 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902405/