In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
C++ how to ensure that destructors do not throw exceptions, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
A destructor may not fail. If a destructor tries to exit with an exception, it's a bad design error and the program had better terminate.
All destructors do not fail. If the destructor tries to throw an exception to exit, this is a serious design error, and a better option is to abort the program.
Note (Note)
A destructor (either user-defined or compiler-generated) is implicitly declared noexcept (independently of what code is in its body) if all of the members of its class have noexcept destructors. By explicitly marking destructors noexcept, an author guards against the destructor becoming implicitly noexcept (false) through the addition or modification of a class member.
If the destructors of all members of a class are noexcept, its destructors (whether user-defined or compiler-generated) are implicitly defined as noexcept (regardless of the specific code in the function body). By explicitly defining the destructor as noexcept, you can prevent the destructor from becoming noexcpet because the class member has been modified.
Example (sample)
Not all destructors are noexcept by default; one throwing member poisons the whole class hierarchy
Not all destructors are noexcept by default; as long as one member throws an exception, it destroys the entire inheritance system.
Struct X {
Details x; / / happens to have a throwing destructor
/ /...
~ X () {} / / implicitly noexcept (false); aka can throw
}
Swipe left and right to see more
So, if in doubt, declare a destructor noexcept.
Therefore, if in doubt, define the destructor as noexcept.
Note (Note)
Why not then declare all destructors noexcept? Because that would in many cases-especially simple cases-be distracting clutter.
Why not define all destructors as noexcept? Because in many situations, especially in simple situations, doing so will only increase the interference of information.
Enforcement (implementation recommendations)
(Simple) A destructor should be declarednoexceptif it could throw.
(simple) if there is a risk of throwing an exception, define the destructor as noexcept.
After reading the above, do you know how to ensure that the destructor does not throw an exception in C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.