The goal of an empty structure?

Auto_ptr declarations from the 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 do not understand the purpose of this part:

 template <class Y> struct auto_ptr_ref {}; 

Without declaring any variable, how can they be valid:

 auto_ptr& operator=(auto_ptr_ref<X>) throw(); 

and also:

 auto_ptr(auto_ptr_ref<X>) throw(); template <class Y> operator auto_ptr_ref<Y>() throw(); 

Edit: as well (I just noticed). I do not understand how the β€œoperator” is used for the last two lines. Isn't the syntax like "return-type operatoroperand;" where is the return type? operand?

+5
source share
2 answers

A Google search for "auto_ptr_ref" shows this detailed explanation .

I do not quite understand this explanation, but it looks like this is for the next. Without this trick, you can pass auto_ptr to a function that takes ownership of the object and assigns your variable to a null pointer. With the extra trick of the class above, you get a compilation error in this case.

+4
source

You copied the text from the wikipedia entry to auto_ptr , right? This is only the open interface auto_ptr and co., And not an excerpt from the implementation. They left the body auto_ptr_ref empty in the article to indicate that there is a note for the library user.

The last two lines:

  template <class Y> operator auto_ptr_ref<Y>() throw(); template <class Y> operator auto_ptr<Y>() throw(); 

- conversion operators. The syntax of the conversion operators is slightly different, because it makes no sense to declare the return type of the conversion operator (this name is already!), So you do not write int operator int(); , but just operator int();

If you need to discuss how auto_ptr_ref is used in auto_ref, SO has a couple of them. For example, here: what is auto_ptr_ref, what does it achieve and how does it reach

+3
source

Source: https://habr.com/ru/post/896958/


All Articles