异常规范

发布于 2024-08-26 06:03:50 字数 71 浏览 5 评论 0原文

我知道这个功能在 C++0x 中将被弃用,但对于我作为一个新手来说,拥有它似乎是一个好主意。谁能向我解释为什么这不是一个好主意?

I know that this feature will be deprecated in C++0x, but for me as a total novice it seems like a good idea to have it. Could anyone explain to me why isn't a good idea?

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

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

发布评论

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

评论(3

两个我 2024-09-02 06:03:50

请参阅 Herb Sutter 撰写的这篇详细文章。他对他们设计中的问题和缺点有最彻底的解释。

务实地了解异常规范

Please see this detailed article by Herb Sutter. He has the most thorough explanation of the problems and short comings of their design.

A Pragmatic Look at Exception Specificiations

呆萌少年 2024-09-02 06:03:50

据我了解,异常规范意味着:

我不希望你(编译器)生成额外的代码来确保异常是这些类型之一。如果没有请终止,我们干杯。每次调用时都需要将额外的检查放入(隐式)异常处理程序(实现它所需的)。

As far as I understand it, an exception specification means:

I wan't you (the compiler) to generate extra code that makes sure that the exception is one of these types. If not call terminate please, we're toast. The extra checking would need to be put into the (implicit) exception handler (required to implement it) in every call.

时间海 2024-09-02 06:03:50

回顾http://www.gotw.ca/publications/mill22.htm

发布第一个:“影子系统”

确实是一个小技术点,而且很容易修复。

问题第二:(错误)理解

以下是许多人认为异常规范的作用:

他的第一点:

  • 保证函数只会抛出列出的异常(可能不会)。

如果这是人们的想法,那很好,因为这正是 ES 所保证的,根据定义。 Herb 在同一份文件中表示同意:

(ES) 在运行时强制函数仅抛出列出的异常(可能没有)。

他的第二点:

  • 基于仅抛出列出的异常(可能没有)的知识,启用编译器优化。

这也是绝对正确的。

他用一个例子解释了为什么第二点是错误的信念:

// Example 1(b) reprise, and two
// potential white lies:
//
int Gunc() throw();    // will throw nothing (?)

int Hunc() throw(A,B); // can only throw A or B (?)

评论正确吗?不完全是。 Gunc() 确实可能会抛出一些东西,而 Hunc() 很可能会抛出 A 或 B 之外的东西!编译器只是保证在它们这样做时将其打得毫无意义......哦,并且大多数情况下也会将您的程序打得毫无意义。

因为 Gunc() 或 Hunc() 确实可能会抛出他们承诺不会抛出的东西,编译器不仅不能假设它不会发生 (...)

Herb 后来说“(ES) 在运行时强制执行 函数只会抛出列出的异常(可能没有)。”也反驳了这个“论点”。

赫伯的两个要点显然、绝对、无可争议地都是错误的,在他看来

我还可以添加什么?

我相信词语都有固定的含义,不能为了“论证”而随意改变。

Review of http://www.gotw.ca/publications/mill22.htm

Issue the First: A “Shadow Type System”

True, minor technical point, and easy to fix.

Issue the Second: (Mis)understandings

Here’s what many people think that exception specifications do:

His first point:

  • Guarantee that functions will only throw listed exceptions (possibly none).

If this is what people think, it is very fine, because it is exactly what ES guarantee, by definition. Herb agrees in the same document:

(ES) Enforce at runtime that functions will only throw listed exceptions (possibly none).

His second point:

  • Enable compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown.

This is also absolutely correct.

He explains why this second point is an incorrect belief with an example:

// Example 1(b) reprise, and two
// potential white lies:
//
int Gunc() throw();    // will throw nothing (?)

int Hunc() throw(A,B); // can only throw A or B (?)

Are the comments correct? Not quite. Gunc() may indeed throw something, and Hunc() may well throw something other than A or B! The compiler just guarantees to beat them senseless if they do… oh, and to beat your program senseless too, most of the time.

Because Gunc() or Hunc() could indeed throw something they promised not to, not only can’t the compiler assume it won’t happen (...)

Herb latter remark that "(ES) Enforce at runtime that functions will only throw listed exceptions (possibly none)." refute this "argument" too.

Both of Herb's 2 main points are obviously, absolutely, indisputably wrong, according to him.

What else can I add?

I believe that words have fix meaning, that can't be changed at will, for the sake of the "argument".

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