空结构背后的目的?

use*_*261 5 c++ pointers

来自C++标准库的auto_ptr声明

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();
};
Run Code Online (Sandbox Code Playgroud)

}

我不明白这部分的目的:

template <class Y> struct auto_ptr_ref {};
Run Code Online (Sandbox Code Playgroud)

在不声明任何变量的情况下,这些变量如何有效:

auto_ptr&                      operator=(auto_ptr_ref<X>) throw();
Run Code Online (Sandbox Code Playgroud)

这些也是:

auto_ptr(auto_ptr_ref<X>) throw();
    template <class Y> operator auto_ptr_ref<Y>() throw();
Run Code Online (Sandbox Code Playgroud)

编辑:还(我只是注意到)我不明白最后两行如何使用"运算符".语法不是"return-type operatoroperand;",返回类型在哪里?操作数?

sha*_*oth 4

谷歌搜索“auto_ptr_ref”揭示了这个详细的解释

我不太明白这个解释,但看起来是为了下面的内容。如果没有这个技巧,您可以将 an 传递auto_ptr到一个函数中,该函数将获得该对象的所有权并将变量分配给空指针。在这种情况下,使用上面的额外类技巧,您将收到编译错误。