破坏者是否按照标准命名? [英] Do Destructors Have Names According To The Standard?

查看:131
本文介绍了破坏者是否按照标准命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标准,类析构函数是否具有意义上的名称?

Do class destructors have names in the pedantic sense according to the Standard?

回想一下构造函数明确没有名称:

Recall that constructors explicitly do not have names:


构造函数没有名称。 A
特殊声明符语法使用

的可选序列函数说明符(7.1.2)后面跟随
的构造函数的类名
后跟参数列表
来声明或定义构造函数。
在这样的声明中,可以忽略构造函数
的可选
圆括号。

Constructors do not have names. A special declarator syntax using an optional sequence of function-specifiers (7.1.2) followed by the constructor’s class name followed by a parameter list is used to declare or define the constructor. In such a declaration, optional parentheses around the constructor class name are ignored.

该标准没有明确说明析构函数有或没有名称,但是有很多参考资料来引用和声明一个使用特殊语言的析构函数,它们都不直接引用析构函数的名字。这个问题似乎在各个地方徘徊:

The Standard does not explicitly state that destructors do or do not have names, but there are many references to how to refer to and declare a destructor using special language, none of which refer directly to the destructor's name. The issue seems to be skirted around in various places:


一个特殊的声明符语法使用
可选的函数说明符(7.1.2)
后跟紧跟着
析构函数的类名后跟一个
空参数列表to
在类
中声明析构函数。

A special declarator syntax using an optional function-specifier (7.1.2) followed by ~ followed by the destructor’s class name followed by an empty parameter list is used to declare the destructor in a class definition.



5.2.4.1:



5.2.4.1:


在一个点之后使用伪析构函数名
。或arrow - > operator
表示由类型名命名的
非类类型的析构函数。
结果只能用作函数调用操作符
()的
操作数,并且这种调用的结果具有
类型void。唯一的效果是在点或箭头之前的后缀表达式

求值。

The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class type named by type-name. The result shall only be used as the operand for the function call operator (), and the result of such a call has type void. The only effect is the evaluation of the postfix-expression before the dot or arrow.



12.4。 12:



12.4.12 :


在显式析构函数调用中,
析构函数名称显示为〜
,名称,命名
析构函数的类类型。
调用析构函数是
的成员
函数(9.3)的通常规则,也就是说,如果
对象不是析构函数的
类类型而不是从析构函数的类类型派生的
类,
程序具有未定义的行为(除了
,它调用对空指针的删除
没有效果)。


In an explicit destructor call, the destructor name appears as a ~ followed by a type-name that names the destructor’s class type. The invocation of a destructor is subject to the usual rules for member functions (9.3), that is, if the object is not of the destructor’s class type and not of a class derived from the destructor’s class type, the program has undefined behavior (except that invoking delete on a null pointer has no effect).

最后一种情况(12.4.12)似乎是对析构函数名称的最直接的引用,但它仍然避免说析构函数一个名字,这是相当含糊的。 12.4.12可以解释为blah是析构函数的名字或者析构函数没有名字,但是你可以将析构函数称为blah。

This last case (12.4.12) seems to be the most direct reference to the destructor's name, but it still avoids saying that the destructor has a name, and is quite ambigious about this. 12.4.12 could be interpreted as "blah is the destructor's name" or as "destructors don't have names, but you can refer to the destructor as blah."

推荐答案

首先,标准在使用name时是矛盾的,我认为。首先,它说(添加了其他形式的名称,由C ++ 0x草案修正)

First of all, the Standard is ambivalent on the use of "name", i think. First, it says (added the other forms of names below, as corrected by the C++0x draft)


名称是一种用法表示实体或标签(6.6.4,6.1)的标识符(2.11),operator-function-id(13.5),conversion-function-id(12.3.2)或template-id(14.2) p>

A name is a use of an identifier (2.11), operator-function-id (13.5), conversion-function-id (12.3.2), or template-id (14.2) that denotes an entity or label (6.6.4, 6.1).

然后在标准的部分,它使用名称,如同包含限定词部分,如 foo :: bar 。而在其他部分,它从名称中排除这些部分。一段甚至表示以 :: 为前缀的名称指的是全局名称。但是在我们的例子中, bar 以这样一个标记为前缀,即使它有意不是指一个全局名。

Then in parts of the Standard it uses "name" as if it would contain qualifier portions like foo::bar. And in other parts, it excludes such portions from a "name". One paragraph even says that a name prefixed by :: refers to a global name. But in our example, bar was prefixed by such a token, even though its intentionally not referring to a global name.

在我们的示例中的构造不是一个名称,我认为,而是两个名称,一个限定另一个。析构函数由构造〜class-name 引用(参见 3.4.5 / 3 3.4.3 / 6 )。这样的结构由一个令牌和一个名称组成,引用构造函数的类。它通常称为析构函数的名称(就像标准在 3.4.3.1/2 谈论一个构造函数名称) - 但在实用上,它不是一个名字。

The construct in our example is not a name, i think, but rather two names, one qualifying the other. A destructor is referenced by the construct ~ class-name (see 3.4.5/3 and 3.4.3/6). Such a construct consists of a ~ token and a name, refering to the constructor's class. It's conventional to call it the destructor's "name" (just like the Standard at 3.4.3.1/2 talks about a "constructor name") - but pedantically, it isn't a name.

因此,如果你是脚本的,你会说一个析构函数没有自己的名称,而是使用特殊的机制来引用它。同样对于构造函数,使用特殊的构造来引用它们(否则,你不能从类声明一个构造函数 - 声明必须引用它)。在C ++ 0x使用声明也必须能够引用它们,使用提供的特殊结构(参见 3.4.3.1/2 如何引用构造函数)。

So, if you are pedantical, you would say that a destructor does not have an own name, but rather special mechanisms are used for referring to it. Likewise for constructors, special constructs are used to refer to them (otherwise, you couldn't declare a constructor out of class - the declaration has to refer to it!). And in C++0x using declarations have to be able to refer to them too, using the special constructs provided (see 3.4.3.1/2 for how you can refer to a constructor).

析构函数查找非常复杂,在标准中有很多错误。有关详细信息,请参见此问题报告

The destructor lookup is quite convoluted, and has quite a few bugs in the Standard. See this issue report for further details.

这篇关于破坏者是否按照标准命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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