【问题标题】:Why does unique_ptr have an overload for auto_ptr?为什么 unique_ptr 对 auto_ptr 有重载?
【发布时间】:2014-09-27 09:05:03
【问题描述】:

我遇到了一个编译器错误并注意到了一些有趣的事情。出于某种原因,unique_ptrauto_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


【解决方案1】:

是的,它是用于与 auto_ptr 互操作的,不推荐使用的方法(根据标准)

当前版本的标准规范,但不保证 在未来的修订中成为标准的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    相关资源
    最近更新 更多