Reputation: 4169
Declarations of auto_ptr from C++ Standard Library
namespace std {
template <class Y> struct auto_ptr_ref {};
template <class X>
class auto_ptr {
public:
typedef X element_type;
// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template <class Y> auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template <class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X>) throw();
~auto_ptr() throw();
// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();
// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y> operator auto_ptr_ref<Y>() throw();
template <class Y> operator auto_ptr<Y>() throw();
};
}
I don't understand the purpose of this part:
template <class Y> struct auto_ptr_ref {};
Without declaring any variable, how can these be valid:
auto_ptr& operator=(auto_ptr_ref<X>) throw();
and these too:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y> operator auto_ptr_ref<Y>() throw();
Edit: and also (I just notice) I don't understand how the "operator" are used for the last two lines. Isn't the syntax something like "return-type operatoroperand;", where is the return type? operand?
Upvotes: 5
Views: 1084
Reputation: 28737
You've copied the text from the wikipedia entry on auto_ptr
, didn't you? This is only the public interface to auto_ptr
and co., not an excerpt from an implementation. They left the auto_ptr_ref
body empty in the article to indicate, that there is noting inside for a user of a library.
The last two lines here:
template <class Y> operator auto_ptr_ref<Y>() throw();
template <class Y> operator auto_ptr<Y>() throw();
are conversion operators. The syntax of conversion operators differs a little, because it makes no sense to declare the return type of a converion operator (it's the name already!), so you don't write int operator int();
but just operator int();
If you need a discussion how auto_ptr_ref is used in auto_ref, SO has a couple of them. For example here: what is auto_ptr_ref, what it achieves and how it achieves it
Upvotes: 3
Reputation: 170479
Google search for "auto_ptr_ref" reveals this detailed explanation.
I don't quite get that explanation, but looks like it is for the following. Without that trick you could pass an auto_ptr
into a function that would get ownership of the object and assign your variable to a null pointer. With the extra class trick above you will get a compile error in such case.
Upvotes: 4