為什么我不能初始化唯一指標
#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]>();
我只是想創建class一個unique 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’
uj5u.com熱心網友回復:
你需要
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(...))where ... 表示轉發傳遞給 make_unique 的 args 非常相似。在這種情況下,用法類似于將一個 arg 轉發到operator new[] (std::size_t size)
1我上面鏈接的檔案并沒有說 arg 是在陣列情況下轉發的,我也不相信規范會這樣做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/429323.html
上一篇:尋找下一個回文
下一篇:澄清陣列和指標的關系
