析构函数不应使用指针来声明吗?在 C++

发布于 2024-12-03 07:48:25 字数 476 浏览 0 评论 0原文

在 C++0x -n3290 草案中:他们在 :Destructors: 12.4/2nd 点最后一行中添加了

          **A destructor shall not be declared with a ref-qualifier.**

在 c++03 草案中....他们没有在析构函数中提到这一点?

我的问题是是否

   *~S() ;   //this declaration is allowed or not according to the Standard's
   //**~S(); ***~S() ; etc...........

允许这种类型的声明? 他在草案中没有描述这个……声明?

在 GCC 4.6.0,Sun/Oracle C++12.0 中,--->允许此声明 int Comeau C/C++ -->不允许

In C++0x -n3290 Draft : they added in section :Destructors : 12.4/2nd point last line

          **A destructor shall not be declared with a ref-qualifier.**

In c++03 Draft .... they didn't mention this point in destructors ?

my question is whether

   *~S() ;   //this declaration is allowed or not according to the Standard's
   //**~S(); ***~S() ; etc...........

this type of declaration is allowed ?
No where in the Draft he described about this ...Declaration?

In GCC 4.6.0,Sun/Oracle C++12.0 , --->this declaration is allowed
int Comeau C/C++ -->not allowed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掐死时间 2024-12-10 07:48:25

这看起来不像是任何类型的合法函数声明,更不用说析构函数了。我不确定标准的这一部分在谈论什么,但我有一个猜测。

我怀疑有一个限定符表示您的函数正在右值引用上调用。像这样的事情:

class A {
 public:
   void IAmAnRValue() &&;
};

我认为标准中的语言是说析构函数上不允许使用此限定符,就像尾随 const 也是非法的一样。

而且,经过进一步的调查,我对自己的猜测的正确性的确信度大大提高了。理由如下:

其中明确指出,函数现在可以在“cv-qualifer”之后有一个“ref-qualifier”。这意味着函数声明现在可以后跟 const &const volatile && 而不仅仅是 const。使用的术语(引用限定符)与您引用的标准中使用的术语相同。析构函数不能拥有析构函数是有道理的。

That doesn't look like it would ever be a legal function declaration of any kind, much less for a destructor. I'm not positive what that part of the standard is talking about, but I have a guess.

I suspect there is a qualifier saying that your function is being called on an rvalue reference. Something like this:

class A {
 public:
   void IAmAnRValue() &&;
};

I think the language in the standard is saying that this qualifier is not allowed on a destructor, much like having a trailing const would also be illegal.

And, on further investigation, my certainty of the correctness of my surmise goes up considerably. Here is the justification:

There it clearly states that functions may now have a 'ref-qualifier' after the 'cv-qualifer'. This means that a function declaration may now be followed by const & or const volatile && instead of just const. And the term used (ref-qualifier) is the same as the term used in the little bit of the standard you're quoting. And it makes sense for destructors to not be able to have one.

打小就很酷 2024-12-10 07:48:25

您误解了新标准中ref-qualifier的含义。就像您可以为 C++03 中的任何成员函数提供 const 限定符一样,您也可以为 C+ 中的成员函数添加 ref-qualifier +0x。该修饰符将影响函数隐式 this 参数的类型:

struct test {
   void f() const &&;  // implicit "this" in "f" is of type "test const &&"
};

就像析构函数不能是 staticconst 一样,或 C++03 中的 const volatile,它不能采用引用限定符&&&) 在 C++0x 中。当然,以前的标准中不存在这一点。

You have misunderstood what ref-qualifier means in the new standard. In the same way that you can provide a const qualifier to any member function in C++03, you can also add a ref-qualifier to a member function in C++0x. That modifier will affect the type of the implicit this argument to the function:

struct test {
   void f() const &&;  // implicit "this" in "f" is of type "test const &&"
};

In the same way that a destructor cannot be static, or const, or const volatile in C++03, it cannot take a ref-qualifier (& or &&) in C++0x. Of course this bit was not present in the former standard.

梦里人 2024-12-10 07:48:25

您正在寻找的规则在同一段中说明,12.4p2

析构函数不接受任何参数,也不能为其指定返回类型(甚至不能指定 void)。

“不能为它指定返回类型”这句话也禁止了“*”,这不是立即清楚的,但可以通过与 12.3.2p1 比较来看出(与 此问题报告):

...这样的函数称为转换函数。无法指定返回类型。

该规则使得实现禁止 * operator int() { }。您也可以与 12.4p1 争论,尽管它的措辞非常笼统,并且是析构函数部分中的第一个语句,我认为上面的其他语句应该是主要参数

使用可选函数说明符 (7.1.2) 的特殊声明符语法,后跟 ~ 后跟析构函数的类名,后跟空参数列表,用于在类定义中声明析构函数。

可以看到/读到,该描述中没有提及诸如 * 之类的声明符,这表明了作者的意图。

The rule you are looking for is stated in the same paragraph, 12.4p2

A destructor takes no parameters, and no return type can be specified for it (not even void).

The phrase "no return type can be specified for it" also forbids "*", which is not immediately clear but can be seen by comparison with 12.3.2p1 (compare with this issue report):

... Such functions are called conversion functions. No return type can be specified.

That rule is what makes implementations forbid * operator int() { }. You can also argue with 12.4p1, although as that's phrased very general and is the first statement in the destructors section, I think the other statement above should be the primary argument

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.

As can be seen/read there is no mention of declarators such as * in that description, which shows the intent of the authors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文