【发布时间】:2014-06-12 13:50:57
【问题描述】:
我正在努力将代码库从 Visual Studio 和 C++10 更新到 VS/C++12
我遇到了一个与 shared_ptr 和 auto_ptr 相关的症结。
原来C++10中的代码是
void CommandLoader::RegisterCommand(std::auto_ptr<ILoadableCommand> cmd)
{
assert(cmd.get() != NULL);
m_pImpl->m_registered.push_back(shared_ptr<ILoadableCommand>(cmd));
}
编译错误是:
error C2440: '<function-style-cast>' : cannot convert from std::auto_ptr<ILoadableCommand>' to 'std::shared_ptr<ILoadableCommand>'
在编辑器内部,它抱怨说
Error: no instance of constructor"std::shared_ptr<_Ty> matches the argument list.
我的猜测是 auto_ptr 不再被接受为 shared_ptr 构造函数的参数,但http://msdn.microsoft.com/en-us/library/bb981959.aspx 另有说法。
我有点茫然,所以任何帮助都会很棒!
【问题讨论】:
-
你可能也应该使用 nullptr,顺便说一句。
-
shared_ptr<ILoadableCommand>(std::move(cmd)). -
谢谢简单。似乎现在可以工作了!
-
还有一件事,将
auto_ptr更改为unique_ptr,因为auto_ptr存在严重缺陷并且已被弃用。
标签: c++ visual-studio visual-c++ visual-studio-2012 visual-c++-2010