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