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

Why is compiler giving error while using make_unique with array?

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

#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]>();

我只是想创建一个 classunique pointer 成员管理 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 );`

... Constructs an array of the given dynamic size. The array elements are value-initialized. This overload participates in overload resolution only if T is an array of unknown bound. The function is equivalent to:

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

Conceptually-speaking1,你可以认为make_unique与[=14=非常相似] 其中 ... 表示转发传递给 make_unique 的参数。在这种情况下,用法是 类似于 将一个参数转发给 operator new[] (std::size_t size)

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