问题描述
根据标准,类析构函数是否具有意义上的名称?
Do class destructors have names in the pedantic sense according to the Standard?
回想一下构造函数明确没有名称:
Recall that constructors explicitly do not have names:
该标准没有明确说明析构函数有或没有名称,但是有很多参考资料来引用和声明一个使用特殊语言的析构函数,它们都不直接引用析构函数的名字。这个问题似乎在各个地方徘徊:
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:
5.2.4.1:
5.2.4.1:
12.4。 12:
12.4.12 :
最后一种情况(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)
然后在标准的部分,它使用名称,如同包含限定词部分,如 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.
这篇关于破坏者是否按照标准命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!