<small id='AJAca'></small><noframes id='AJAca'>

    <bdo id='AJAca'></bdo><ul id='AJAca'></ul>

    <tfoot id='AJAca'></tfoot>

      <legend id='AJAca'><style id='AJAca'><dir id='AJAca'><q id='AJAca'></q></dir></style></legend>

    1. <i id='AJAca'><tr id='AJAca'><dt id='AJAca'><q id='AJAca'><span id='AJAca'><b id='AJAca'><form id='AJAca'><ins id='AJAca'></ins><ul id='AJAca'></ul><sub id='AJAca'></sub></form><legend id='AJAca'></legend><bdo id='AJAca'><pre id='AJAca'><center id='AJAca'></center></pre></bdo></b><th id='AJAca'></th></span></q></dt></tr></i><div id='AJAca'><tfoot id='AJAca'></tfoot><dl id='AJAca'><fieldset id='AJAca'></fieldset></dl></div>
    2. C++03 throw() 说明符 C++11 noexcept 之间的区别

      时间:2023-08-03

        • <i id='oM0FB'><tr id='oM0FB'><dt id='oM0FB'><q id='oM0FB'><span id='oM0FB'><b id='oM0FB'><form id='oM0FB'><ins id='oM0FB'></ins><ul id='oM0FB'></ul><sub id='oM0FB'></sub></form><legend id='oM0FB'></legend><bdo id='oM0FB'><pre id='oM0FB'><center id='oM0FB'></center></pre></bdo></b><th id='oM0FB'></th></span></q></dt></tr></i><div id='oM0FB'><tfoot id='oM0FB'></tfoot><dl id='oM0FB'><fieldset id='oM0FB'></fieldset></dl></div>
            <bdo id='oM0FB'></bdo><ul id='oM0FB'></ul>

                <tbody id='oM0FB'></tbody>
              <legend id='oM0FB'><style id='oM0FB'><dir id='oM0FB'><q id='oM0FB'></q></dir></style></legend>
              <tfoot id='oM0FB'></tfoot>

                <small id='oM0FB'></small><noframes id='oM0FB'>

                本文介绍了C++03 throw() 说明符 C++11 noexcept 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                throw()noexcept 除了分别在运行时和编译时检查之外,还有什么区别吗?

                Is there any difference between throw() and noexcept other than being checked at runtime and compile time, respectively?

                这篇维基百科 C++11 文章表明 C++03 抛出说明符已弃用.
                为什么是这样,noexcept 是否足以在编译时涵盖所有这些内容?

                This Wikipedia C++11 article suggests that the C++03 throw specifiers are deprecated.
                Why so, is noexcept capable enough to cover all that at compile time ?

                [注意:我检查了这个问题和这篇文章,但无法确定弃用的确切原因.]

                [Note: I checked this question and this article, but couldn't determine the solid reason for deprecation.]

                推荐答案

                异常说明符已被弃用,因为异常说明符通常是一个糟糕的主意.添加 noexcept 是因为它是异常说明符的一个相当有用的用法:知道函数何时不会抛出异常.因此它变成了一个二元选择:会抛出的函数和不会抛出的函数.

                Exception specifiers were deprecated because exception specifiers are generally a terrible idea. noexcept was added because it's the one reasonably useful use of an exception specifier: knowing when a function won't throw an exception. Thus it becomes a binary choice: functions that will throw and functions that won't throw.

                noexcept 而不是删除除 throw() 之外的所有 throw 说明符,因为 noexcept 更强大.noexcept 可以有一个编译时解析为布尔值的参数.如果布尔值为真,则 noexcept 坚持.如果布尔值为 false,则 noexcept 不会粘住,函数可能会抛出.

                noexcept was added rather than just removing all throw specifiers other than throw() because noexcept is more powerful. noexcept can have a parameter which compile-time resolves into a boolean. If the boolean is true, then the noexcept sticks. If the boolean is false, then the noexcept doesn't stick and the function may throw.

                因此,您可以执行以下操作:

                Thus, you can do something like this:

                struct<typename T>
                {
                  void CreateOtherClass() { T t{}; }
                };
                

                CreateOtherClass 会抛出异常吗?它可能,如果 T 的默认构造函数可以.我们怎么讲?像这样:

                Does CreateOtherClass throw exceptions? It might, if T's default constructor can. How do we tell? Like this:

                struct<typename T>
                {
                  void CreateOtherClass() noexcept(is_nothrow_default_constructible<T>::value) { T t{}; }
                };
                

                因此,如果给定类型的默认构造函数抛出,CreateOtherClass() 将抛出.这解决了异常说明符的主要问题之一:它们无法向上传播调用堆栈.

                Thus, CreateOtherClass() will throw iff the given type's default constructor throws. This fixes one of the major problems with exception specifiers: their inability to propagate up the call stack.

                你不能用 throw() 做到这一点.

                You can't do this with throw().

                这篇关于C++03 throw() 说明符 C++11 noexcept 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:应该捕获 boost::asio::io_service::run() 抛出的异常吗 下一篇:从构造函数的初始化列表中捕获异常

                相关文章

                最新文章

                <i id='r35SQ'><tr id='r35SQ'><dt id='r35SQ'><q id='r35SQ'><span id='r35SQ'><b id='r35SQ'><form id='r35SQ'><ins id='r35SQ'></ins><ul id='r35SQ'></ul><sub id='r35SQ'></sub></form><legend id='r35SQ'></legend><bdo id='r35SQ'><pre id='r35SQ'><center id='r35SQ'></center></pre></bdo></b><th id='r35SQ'></th></span></q></dt></tr></i><div id='r35SQ'><tfoot id='r35SQ'></tfoot><dl id='r35SQ'><fieldset id='r35SQ'></fieldset></dl></div>
                  <bdo id='r35SQ'></bdo><ul id='r35SQ'></ul>
              • <small id='r35SQ'></small><noframes id='r35SQ'>

                    <tfoot id='r35SQ'></tfoot>

                    <legend id='r35SQ'><style id='r35SQ'><dir id='r35SQ'><q id='r35SQ'></q></dir></style></legend>