Mr.*_*C64 11 c++ unique-ptr c++11 c++14
我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎没有用make_unique(); 一个小的可编辑的repro代码片段如下:
#include <memory>
using namespace std;
int main() {
unique_ptr<int[]> p = make_unique<int[]>(3);
p.reset(make_unique<int[]>(10));
}
Run Code Online (Sandbox Code Playgroud)
从命令行编译:
Run Code Online (Sandbox Code Playgroud)C:\Temp\CppTests>cl /EHsc /W4 /nologo test.cpp
这些是来自MSVC编译器的错误:
Run Code Online (Sandbox Code Playgroud)test.cpp(6) : error C2280: 'void std::unique_ptr<int [],std::default_delete<_Ty> >::reset<std::unique_ptr<_Ty,std::default_delete<_Ty>>>(_Ptr2)' : attempting to reference a deleted function with [ _Ty=int [] , _Ptr2=std::unique_ptr<int [],std::default_delete<int []>> ] C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\memory(16 23) : see declaration of 'std::unique_ptr<int [],std::default_delete<_Ty>>::rese t' with [ _Ty=int [] ]
但是,以下代码似乎编译良好:
p = make_unique<int[]>(10);
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?为什么要unique_ptr::operator=()合作make_unique(),但unique_ptr::reset()不是?
seh*_*ehe 18
reset() 拿一个指针.
你似乎想要的是简单的移动任务:
#include <memory>
using namespace std;
int main() {
unique_ptr<int[]> p = make_unique<int[]>(3);
p = make_unique<int[]>(10);
}
Run Code Online (Sandbox Code Playgroud)
有些编译器可能仍然希望您指定std::move()那里,但并不是严格要求的.