c++ - 如何在 C++11 中实现 make_unique 函数?

标签 c++ c++11 unique-ptr c++14

我的编译器不支持 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/

相关文章:

c++ - 在没有对象的情况下更改 class::variable 的默认值

c++ - 获取具有计数的 vector 的不同 vector

c++ - C++ 编译器如何在 C++0x 中实现线程本地存储?

c++ - 如何在 Qt Creator 中启用 C++11?

c++-cli - 在 header 中声明 unique_Ptr 变量,然后在构造函数中稍后分配它的语法是什么?

c++ - qml 未知方法返回类型 : ArchiveFile*, 即使调用了 qmlRegisterUncreatableType

c++ - GDB:包含 STL 容器的 Pretty-Print 类

c++ - 涉及 const unique_ptr 的 move 构造函数

从 Visual Studio 2010 迁移到 2012 时的 C++11 问题

c++ - C/C++ 中的双向链表与多链表