如何从vector向量pop_back共享指针并转换为unique_ptr

Mic*_*ele 1 c++ vector shared-ptr unique-ptr

我正在尝试从我的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())));
}
Run Code Online (Sandbox Code Playgroud)

IFCCB.h:

public:
unique_ptr<IFC> getElementVectorIFC();
Run Code Online (Sandbox Code Playgroud)

编译错误:

错误C2784:'enable_if :: value,std :: unique_ptr <_Ty,std :: default_delete <_Ty >>> :: type std :: make_unique(_Types && ...)':无法推断'_Types &&的模板参数'from'void'

据我所知,我正在做我在别处看到的事情.

我查看了make_unique信息,但它没有给出一个非常好的示例,并且使用了unique_ptr.有任何想法吗?

Mik*_*our 5

您无法将所有权从共享指针转移到唯一指针.共享所有权后,它仍然是共享的.

如果您不需要与容器共享所有权,则存储唯一指针,或者如果您从容器中删除,则继续使用共享指针.