入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2016-04-27 投稿人: 浏览次数:270

虽然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à!

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像