函数try catch语法和main [英] function try catch syntax and main

查看:248
本文介绍了函数try catch语法和main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个鲜为人知的,但几乎从未使用的C ++特性被赋予一个声明:

A little known, but almost never used C++ feature is given a declaration:

void foo();

一个可能的法律定义可能是:

One possible, legal definition could be:

void foo() try {
  throw 42;
}
catch(...) {
}

这里,整个函数实现被包装在 try / catch pair ,这似乎与允许

Here the whole function implementation wrapped is within a try/catch pair, which seems to be similar to allowing this.

这是合法的 int main() ?例如:

int main() try {
  throw 42;
}
catch(...) {
}

主要规则,n3290§3.6.1主要讨论的是它应该采取什么,它返回什么 - 它们似乎并不明确禁止它,因为它们与各种其他奇怪的事情(例如链接),你可能会试着尝试。

The rules for main, n3290 § 3.6.1 mostly talk about what arguments it should take and what it returns - they don't seem to explicitly forbid it as they do with various other odd things (e.g. linkages) you might be tempted to try.

这是合法和定义良好的吗?

Is this legal and well defined?

推荐答案

标准不禁止其在[basic.start.main]并强制所有实现支持至少 int main(){/*...*/} int main(int argc,char * argv []){/*...*/},不限制实现这两个声明(3.6.1,第2段)。

The standard does not forbid its usage within [basic.start.main], and, while forcing all implementations to support at least int main() {/*...*/ } and int main(int argc, char* argv[]) {/*...*/}, does not limit implementations to those two declarations (3.6.1, para. 2).

从孤立的角度来看,它至少是合法的,虽然当然只涉及函数声明,而不是函数定义。

From that in isolation, it would appear at the least that it is legal, though of course that relates only to function-declarations, not function-definitions.

Reading on,[except.handle],第13段规定如下:

Reading on, [except.handle], paragraph 13 states the following:


静态存储对象的析构函数抛出异常
持续时间或在命名空间范围对象的构造函数中没有通过main()上的函数try块阻止
。 (15.3第13段)

Exceptions thrown in destructors of objects with static storage duration or in constructors of namespace-scope objects are not caught by a function-try-block on main(). (15.3 para. 13)

它特别提到了 function-try-block code> main(),这强烈地暗示这样的结构是合法的并且已经定义了行为。在信息中添加 main()只是在其名称和返回类型中是特殊的,并且实现可能不会重载它来改变任何行为,使得一个非常强的情况除非特别注明,例如在上面的报价中,以正常的方式行事。

It makes specific mention of a function-try-block placed on main(), which strongly implies that such a structure is legal and has defined behavior. Adding in the information that main() is only special in its name and return type, and that implementations may not overload it to alter any behavior, makes a pretty strong case that it acts in a normal fashion except when specially noted such as in the above quote. In other words, yes, it is legal and well-defined.

我在这个答案的第一个版本中提供的博文实际上是很好地说明了上述blockquote中给出的规则,因此我将保留

The blog post I supplied in the first version of this answer actually does a good job of illustrating the rules given by the above blockquote, so I'll retain the link to it, even though it does not directly discuss the issue in the OP's question.

对于OP的评论,你可以发出return语句在 function-try-block 中,和[except.handle]有这样的说法:

Regarding a comment on the OP, you can issue return statements within a function-try-block, and [except.handle] has this to say:


一个函数try块的结束相当于没有值的返回
;这导致在返回
函数(6.6.3)中的未定义行为。 (15.3第15段)

Flowing off the end of a function-try-block is equivalent to a return with no value; this results in undefined behavior in a value-returning function (6.6.3). (15.3 para. 15)

如果你在 main ,你不会流过函数体(这将是在这种情况下的try块),所以规则main自动调用 return 0; 不适用。您需要返回一些 int (很可能是一个错误代码),避免变得未定义。

If you're in a catch-block at the end of main, you're not going to flow over the function's body (which would be the try-block in this case), so the rule that main automatically calls return 0; on flowover doesn't apply. You need to return some int (quite possibly an error code) to keep from becoming undefined.

这篇关于函数try catch语法和main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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