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

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

问题描述

有一点知道,但几乎从未使用C ++功能给出了一个声明:

  void foo(); 

一个可能的合法定义可以是:

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

这里包含的整个功能实现在 try / catch pair ,这似乎类似于允许这个



对于 int main() ?例如:

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

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



这个法律和定义是否合法?

解决方案

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



从这一点来看,至少这是合法的,尽管当然只涉及函数声明而不是功能定义。



阅读, [except.handle],第13段说明如下:


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


它具体提到了一个放在 main(),这强烈意味着这样的结构是合法的,并且已经定义了行为。添加 main()的信息仅在其名称和返回类型中特殊,并且该实现可能无法重载以改变任何行为,使得它非常强大以正常方式行事,除非特别注明,如上述报价。换句话说,是的,它是合法和明确的。



我在这个答案的第一个版本中提供的博文实际上是很好地说明了上述blockquote给出的规则,所以我将保留链接到它,即使它不直接讨论OP的问题。



关于对OP的评论,您可以发出返回语句在 function-try-block 中,[except.handle]可以这样说:


流出function-try-block的结尾相当于没有值的return
;这导致返回
函数(6.6.3)中的未定义的行为。 (15.3第15段)


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


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(...) {
}

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

Is that legal to do for int main()? E.g.:

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

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?

解决方案

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], paragraph 13 states the following:

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)

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.

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.

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

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)

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天全站免登陆