std ::退出歧义?

编程入门 行业动态 更新时间:2024-10-26 06:27:43
本文介绍了std ::退出歧义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

摘自标准: 4调用函数 void exit(int); 在< cstdlib>中声明(18.3)终止程序而不离开 当前块,因此没有 销毁任何具有自动存储持续时间(12.4)的对象。如果在 期间调用退出 来破坏具有静态存储持续时间的对象,则该程序具有 未定义的行为。 br /> 现在使用以下代码: #include< cstdlib> #include< iostream> class Blah { public: ~Blah() { std :: cout<< \ nBlah'的析构函数!\ n" ;; } }; int main() { Blah blah; {exit(0); } } 我自己会认为blah'的析构函数*会被调用... 毕竟,它的范围不是当前块。 想法? -JKop

解决方案

下面的析构函数也没有被调用: #include< cstdlib> #include< iostream> 无效牛() { 退出(0); } int main() { Blah blah; Cow() ; }

" JKop" < NU ** @ NULL.NULL>在消息中写道 新闻:Qx ******************* @ news.indigo.ie ...

标准摘录: 4调用函数 void exit(int); 在< cstdlib>中声明(18.3)终止程序而不离开当前块,因此不会破坏任何具有自动存储持续时间的对象(12.4)。如果在销毁具有静态存储持续时间的对象期间调用exit来结束程序,则该程序具有未定义的行为。 现在使用以下代码: #include< cstdlib> #include< iostream> 班级Blah {公开: ~Blah() { std :: cout<< \ nBlah'的析构函数!\ n"; } }; int main() { Blah blah; {exit(0); } } 我自己也会认为这个问题的析构函数*会被称为b $ b ...毕竟,它的范围不是那个当前区块。 想法?

我认为你误读了这个标准。它没有说破坏 取决于对象是否在当前块中。它表示 自动对象的析构函数不会被调用,因为当前块 不会被遗忘。换句话说,没有自动对象的析构函数将被调用,期间。 如果你想结束一个程序并拥有名为 然后抛出异常。 john

>> 4调用函数

void exit(int); 在< cstdlib>中声明(18.3)终止程序而不离开当前块,因此不会破坏任何具有自动存储持续时间的对象(12.4)。如果在具有静态存储持续时间的对象被销毁期间调用exit 来结束程序,则该程序具有未定义的行为。 我认为你''重新误读标准。它并没有说破坏取决于物体是否在当前区块中。它说自动对象的析构函数不会被调用,因为当前块不会被删除。换句话说,没有用于自动对象的析构函数,句号。

嗯......是的......我想......但是我如果标准更明确,那么我更愿意。例如: 不会破坏任何和所有带自动存储的对象,包括 其他块中定义的那些。 如果你想要结束一个程序并有自动对象析构器调用然后抛出异常。 john

一如既往地有帮助,谢谢! 你的意思是抛出异常并没有进入? 我正在做的是从main调用一个函数。我希望这个功能具有功率和功率。结束程序。 如下: void SomeFunc() { //出错了 throw int(); } int main() { SomeFunc(); } 如果没有发现异常会发生什么......我把它带到某处 " std ::"函数叫,是吗? 然而再看看,我的代码中没有任何析构函数被调用 以上,还有什么。 ..错......跟退出电话?让我们说 实例,你有一个std :: string;具有自动存储的对象,如果你调用exit()你调用UB是不是通过调用它的析构函数来调用UB?并且 即使析构函数没有被调用,内存是否仍然被释放为 对象? -JKop

An excerpt from the Standard: 4 Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior. Now take the following code: #include <cstdlib> #include <iostream> class Blah { public: ~Blah() { std::cout << "\nBlah''s Destructor!\n"; } }; int main() { Blah blah; { exit(0); } } I myself would''ve thought that blah''s destructor *would* have been called... after all, its scope isn''t that of "the current block". Thoughts? -JKop

解决方案

The destructor doesn''t get called in the following either: #include <cstdlib> #include <iostream> void Cow() { exit(0); } int main() { Blah blah; Cow(); }

"JKop" <NU**@NULL.NULL> wrote in message news:Qx*******************@news.indigo.ie...

An excerpt from the Standard: 4 Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior. Now take the following code: #include <cstdlib> #include <iostream> class Blah { public: ~Blah() { std::cout << "\nBlah''s Destructor!\n"; } }; int main() { Blah blah; { exit(0); } } I myself would''ve thought that blah''s destructor *would* have been called... after all, its scope isn''t that of "the current block". Thoughts?

I think you''re misreading the standard. It doesn''t say that destruction depends upon whether an object is in the current block or not. It says that destructors for automatic objects are not called BECAUSE the current block isn''t left. In other words no destructors for automatic objects will be called, period. If you want to end a program and have automatic object destructors called then throw an exception. john

>> 4 Calling the function

void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior. I think you''re misreading the standard. It doesn''t say that destruction depends upon whether an object is in the current block or not. It says that destructors for automatic objects are not called BECAUSE the current block isn''t left. In other words no destructors for automatic objects will be called, period.

Hmm... yeah... I suppose... but I''d prefer if the Standard were more explicit. For example: without destroying any and all objects with automatic storage, including those defined in other blocks. If you want to end a program and have automatic object destructors called then throw an exception. john

Helpful as ever, thanks! You mean throw an exception and not catch in? What I''m doing is calling a function from main. I want this function to have the "power" to end the program. As in: void SomeFunc() { //something goes wrong throw int(); } int main() { SomeFunc(); } What happens when an exception isn''t caught... I take it there''s a certain "std::" function called, yeah? But then again, seeing as how there''s no destructors to be called in my code above, is there anything... wrong... with calling exit? And let''s say for instance that you have an "std::string" object with automatic storage, if you call exit() are you invoking UB by not having its destructor called? And even though the destructor isn''t called, is the memory still deallocated for the object? -JKop

更多推荐

std ::退出歧义?

本文发布于:2023-11-07 06:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!