空结构背后的目的是什么? [英] The purpose behind empty struct?

查看:94
本文介绍了空结构背后的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准库中对auto_ptr的声明

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();

还有这些:

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

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

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?

推荐答案

在Google搜索"auto_ptr_ref"时显示

Google search for "auto_ptr_ref" reveals this detailed explanation.

我还不太明白,但下面的内容看起来像是.没有这种技巧,您可以将auto_ptr传递到一个函数中,该函数将获得对象的所有权并将变量分配给空指针.通过上面的附加类技巧,在这种情况下,您将得到一个编译错误.

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.

这篇关于空结构背后的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆