我正在尝试从我的 vector 中弹出我的 shared_pointer 并转换为 unique_ptr。不幸的是,它给出了一条奇怪的编译信息。
IFCCB.cpp:
std::unique_ptr<IFC> IFCCCB::getElementVectorIFC()
{
return (std::unique_ptr<IFC>(make_unique<IFC>(m_shpVectorIFC.pop_back())));
}
IFCCB.h:
public:
unique_ptr<IFC> getElementVectorIFC();
编译错误:
error C2784: 'enable_if::value,std::unique_ptr<_Ty,std::default_delete<_Ty>>>::type std::make_unique(_Types &&...)' : could not deduce template argument for '_Types &&' from 'void'
据我所知,我正在做我在其他地方看到的事情。
我看了make_unique info但它并没有给出一个很好的例子,而且unique_ptr use .有什么想法吗?
最佳答案
您不能将所有权从共享指针转移到唯一指针。一旦共享所有权,它就会保持共享状态。
如果您不需要与容器共享所有权,则存储唯一指针,或者如果需要,在从容器中移除后继续使用共享指针。
关于c++ - 如何从 vector 中弹出共享指针并转换为 unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27823691/