../UniqueStrTest/main.cpp: In function ‘int main(int, char**)’:
../UniqueStrTest/main.cpp:35:25: error: ‘make_unique’ is not a member of ‘std’
auto p = std::make_unique<D>(); // p 是占有 D 的 unique_ptr
^~~~~~~~~~~
make_unique从c++14开始,c++11不支持,但是可以自己实现make_unique

/// std::make_unique for single objects
template<typename _Tp, typename... _Args>
inline typename _MakeUniq<_Tp>::__single_object
make_unique(_Args&&... __args)
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
/// std::make_unique for arrays of unknown bound
template<typename _Tp>
inline typename _MakeUniq<_Tp>::__array
make_unique(size_t __num)
{ return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
/// Disable std::make_unique for arrays of known bound
template<typename _Tp, typename... _Args>
inline typename _MakeUniq<_Tp>::__invalid_type
make_unique(_Args&&...) = delete;
C++11中缺失的make_unique:手动实现
这篇博客讨论了C++11标准中`std::make_unique`的缺失,并提供了如何在C++11环境中手动实现`make_unique`的代码示例,以创建单个对象和动态数组的智能指针。
2493

被折叠的 条评论
为什么被折叠?



