std::swap 不支持 std::auto_ptr 类型参数

虽然auto_ptr即将成为历史,但公司内的代码还是有大量的使用。

在为一个用pimpl模式实现的class 写swap函数的时候,偶然发现std::swap不支持auto_ptr<T>类型的参数:在gcc 3.4下不能通过编译。

为什么呢?

网上的一个对话给出了答复:https://gcc.gnu.org/ml/libstdc++/2004-11/msg00086.html

According to 20.4.5, p3, literally:
"auto_ptr does not meet the CopyConstructible and Assignable requirements..."
and, according to 25.2.2, p1 (about swap(T& a, T& b)):
"Requires: Type T is CopyConstructible and Assignable"

因为auto_ptr的operator=和拷贝构造函数的参数是一个非const引用,不满足可赋值性,也不满足拷贝构造性,所以用标准的swap函数交换两个auto_ptr变量,产生编译错误是完全正常的。

再看gcc 3.4的代码:

template<typename _Tp>
inline void
swap(_Tp& __a, _Tp& __b)
{
// concept requirements
__glibcpp_function_requires(_SGIAssignableConcept<_Tp>)
const _Tp __tmp = __a; // <-- 产生编译错误,因为auto_ptr没有接受const reference的拷贝构造函数
__a = __b;
__b = __tmp;
}

解决方法是自己实现一个特殊的swap

template<typename __Tp>
void swap_autoptr(auto_ptr<__Tp> & __a, auto_ptr<__Tp> & __b)
{
auto_ptr<__Tp> __tmp = __a;
__a = __b;
__b = __tmp;
}

voilà!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值