为什么编译器在对数组使用make_unique时出错



为什么我不能初始化一个唯一的指针

#include <iostream>
#include <memory>
class Widget
{
std::unique_ptr<int[]> arr;

public:
Widget(int size)
{
arr = std::make_unique<int[size]>();
}
~Widget()
{

}
};
int main()
{

}

我无法理解以下错误信息的含义。我调用有什么问题arr = std::make_unique<int[size]>();

我只是想创建一个具有unique pointer成员的class来管理array。我应该如何更改以下程序以及为什么?

Error(s):
1129699974/source.cpp: In constructor ‘Widget::Widget(int)’:
1129699974/source.cpp:13:43: error: no matching function for call to ‘make_unique<int [size]>()’
arr = std::make_unique<int[size]>();
^
In file included from /usr/include/c++/7/memory:80:0,
from 1129699974/source.cpp:4:
/usr/include/c++/7/bits/unique_ptr.h:824:5: note: candidate: template<class _Tp, class ... _Args> typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...)
make_unique(_Args&&... __args)
^~~~~~~~~~~
/usr/include/c++/7/bits/unique_ptr.h:824:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/bits/unique_ptr.h: In substitution of ‘template<class _Tp, class ... _Args> typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = int [size]; _Args = {}]’:
1129699974/source.cpp:13:43:   required from here
/usr/include/c++/7/bits/unique_ptr.h:824:5: error: ‘int [size]’ is a variably modified type
/usr/include/c++/7/bits/unique_ptr.h:824:5: error:   trying to instantiate ‘template<class _Tp> struct std::_MakeUniq’
/usr/include/c++/7/bits/unique_ptr.h:830:5: note: candidate: template<class _Tp> typename std::_MakeUniq<_Tp>::__array std::make_unique(std::size_t)
make_unique(size_t __num)
^~~~~~~~~~~
/usr/include/c++/7/bits/unique_ptr.h:830:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/bits/unique_ptr.h: In substitution of ‘template<class _Tp> typename std::_MakeUniq<_Tp>::__array std::make_unique(std::size_t) [with _Tp = int [size]]’:
1129699974/source.cpp:13:43:   required from here
/usr/include/c++/7/bits/unique_ptr.h:830:5: error: ‘int [size]’ is a variably modified type
/usr/include/c++/7/bits/unique_ptr.h:830:5: error:   trying to instantiate ‘template<class _Tp> struct std::_MakeUniq’
/usr/include/c++/7/bits/unique_ptr.h:836:5: note: candidate: template<class _Tp, class ... _Args> typename std::_MakeUniq<_Tp>::__invalid_type std::make_unique(_Args&& ...) <deleted>
make_unique(_Args&&...) = delete;
^~~~~~~~~~~
/usr/include/c++/7/bits/unique_ptr.h:836:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/bits/unique_ptr.h: In substitution of ‘template<class _Tp, class ... _Args> typename std::_MakeUniq<_Tp>::__invalid_type std::make_unique(_Args&& ...) [with _Tp = int [size]; _Args = {}]’:
1129699974/source.cpp:13:43:   required from here
/usr/include/c++/7/bits/unique_ptr.h:836:5: error: ‘int [size]’ is a variably modified type
/usr/include/c++/7/bits/unique_ptr.h:836:5: error:   trying to instantiate ‘template<class _Tp> struct std::_MakeUniq’

您需要

arr = std::make_unique<int[]>(size);

make_unique有一个特定的记录用途:

template< class T > unique_ptr<T> make_unique( std::size_t size );`

构造一个给定动态大小的数组。数组元素已进行值初始化。只有当T是未知界的数组时,此重载才参与重载解析。该功能相当于:

unique_ptr<T>(new std::remove_extent_t<T>[size]())

从概念上讲1,您可以认为make_unique与std::unique_ptr<T>(new T(...))非常相似,其中。。。表示传递给make_unique的args的转发。在这种情况下,用法类似于将一个arg转发到operator new[] (std::size_t size)

1我上面链接的文档并没有说arg是在数组的情况下转发的,我也不相信规范会这样做。

最新更新