【发布时间】:2014-09-27 09:05:03
【问题描述】:
我遇到了一个编译器错误并注意到了一些有趣的事情。出于某种原因,unique_ptr 对 auto_ptr 有重载,但我认为 auto_ptr 已被弃用:
/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:
note:template<class _Up, class>
std::unique_ptr<_Tp, _Dp>::unique_ptr(std::auto_ptr<_Up>&&)
unique_ptr(auto_ptr<_Up>&& __u) noexcept;
/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:
note: template argument deduction/substitution failed:
main.cpp:41:67: note: mismatched types 'std::auto_ptr<T>' and 'char*'
这是因为向后兼容使用 auto_ptr 的代码吗?
【问题讨论】:
-
除了向后兼容使用
auto_ptr的代码外,它还可以用作从auto_ptr移动到unique_ptr的转换工具。不必一次转换整个代码库。您可以一次完成一块,使用此转换跨越过渡边界。话虽如此,现场报告表明,作为过渡方法,仅进行全局搜索/替换然后调查编译时失败的成功率很高。
标签: c++ c++11 unique-ptr auto-ptr