2
template<typename _Tp1>
operator auto_ptr_ref<_Tp1>() throw()
{ return auto_ptr_ref<_Tp1>(this->release()); }

template<typename _Tp1>
operator auto_ptr<_Tp1>() throw()
{ return auto_ptr<_Tp1>(this->release()); }

我在 stl 类 auto_ptr 中找到了这两种方法的定义。

有人可以解释一下,构造函数以外的函数如何没有返回类型?

4

2 回答 2

6

因为它们是分别返回auto_ptr_ref<_Tp1>和的隐式转换运算符auto_ptr<_Tp1>。这些本身用作返回类型声明。

于 2012-09-01T20:41:59.397 回答
2

将运算符转换为类型auto_ptr_refauto_ptr

n3337 12.3.2/1

类 X 的成员函数,没有参数,名称为

转换函数 ID:运算符转换类型 ID

转换类型 ID:类型说明符序列转换声明器选择

转换声明符:ptr 运算符转换声明符opt

指定从 X 到由 conversion-type-id 指定的类型的转换

于 2012-09-01T20:42:20.113 回答