• <small id='wxOw1'></small><noframes id='wxOw1'>

    <tfoot id='wxOw1'></tfoot>

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

          <bdo id='wxOw1'></bdo><ul id='wxOw1'></ul>
      1. 可能 STL 迭代器方法抛出异常

        时间:2024-08-14

        <small id='2AcCA'></small><noframes id='2AcCA'>

        1. <i id='2AcCA'><tr id='2AcCA'><dt id='2AcCA'><q id='2AcCA'><span id='2AcCA'><b id='2AcCA'><form id='2AcCA'><ins id='2AcCA'></ins><ul id='2AcCA'></ul><sub id='2AcCA'></sub></form><legend id='2AcCA'></legend><bdo id='2AcCA'><pre id='2AcCA'><center id='2AcCA'></center></pre></bdo></b><th id='2AcCA'></th></span></q></dt></tr></i><div id='2AcCA'><tfoot id='2AcCA'></tfoot><dl id='2AcCA'><fieldset id='2AcCA'></fieldset></dl></div>
              <bdo id='2AcCA'></bdo><ul id='2AcCA'></ul>
                <tbody id='2AcCA'></tbody>

              • <tfoot id='2AcCA'></tfoot>

                1. <legend id='2AcCA'><style id='2AcCA'><dir id='2AcCA'><q id='2AcCA'></q></dir></style></legend>
                2. 本文介绍了可能 STL 迭代器方法抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  析构函数可能不会抛出异常(所以 stack unwinding 可以在异常处理期间完成),并且必须释放分配给对象的任何资源(因此不会出现资源泄漏).包含多个其他对象(或分配了多个资源)的对象的设计可能会在 STL 容器中记录指向它们的指针.因此,析构函数将使用以下与迭代器相关的方法:

                  Destructors may not throw exceptions (so stack unwinding can complete during exception handling), and must deallocate any resources allocated to the object (so no resources leak). A design for an object that contains several other objects (or is allocated several resources) might record pointers to them in an STL container. The destructor would therefore use the following iterator-related methods:

                  • begin(), end() 用于容器
                  • operator++ 用于有效迭代器
                  • operator*operator-> 用于有效迭代器
                  • begin(), end() for the container
                  • operator++ for a valid iterator
                  • operator* or operator-> for a valid iterator

                  但是为了保证析构函数既不抛出异常又不释放其资源,您需要依赖那些从不抛出异常的方法.

                  But to guarantee that the destructor both does not throw exceptions and deallocates its resources you would need to rely on those methods never throwing exceptions.

                  依赖那些从不抛出异常的方法是否安全?很难想象会抛出异常的实际实现,因为在底层,STL 迭代器本质上是一个指针.但是标准 C++ 是否要求这些方法永远不会抛出异常?我在 C++ 标准中没有找到明确的声明.

                  Is it safe to rely on those methods never throwing exceptions? It is hard to imagine a practical implementation that would throw exceptions, as under the hood an STL iterator is essentially a pointer. But does standard C++ require that those methods never throw exceptions? I've not found a clear statement in the C++ standard.

                  编辑:当您想要 一个容器指针到资源.这样做是有充分理由的;例如,如果您有多态资源.正如 Bjrn Pollex 在他的回答中指出的那样,如果您使用资源容器(例如 std::list< Resource >)而不是资源指针容器,容器的析构函数将负责销毁(解除分配)Resource 对象.

                  Edit: The interesting case is for C++ 03 when you want to have a container of pointers to resources. There are good reasons for doing this; for example, if you have polymorphic resources. As Bjrn Pollex points out in his answer, if you use a container of resources (such as a std::list< Resource >) rather than a container of pointers to resources, the destructor of the container will take care of destruction (deallocation) of the Resource objects for you.

                  推荐答案

                  operator++ 用于有效迭代器

                  operator++ for a valid iterator

                  C++ 标准(我指的是 N3290 草案)并没有为迭代器的增量运算符提供 nothrow 保证.

                  The C++ standard (I refer to N3290 draft) does not give nothrow guarantee for increment operator of iterators.

                  例如,std::istreambuf_iterator::operator++std::basic_streambuf::sbumpc 的调用有影响.sbumpc 可能会调用 uflow 而后者可能会抛出异常.

                  For example, std::istreambuf_iterator::operator++ effects in call to std::basic_streambuf::sbumpc. The sbumpc may call uflow which in turn may throw exception.

                  这篇关于可能 STL 迭代器方法抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何找到任何对象使用的内存 下一篇:为什么要在 C++ 中使用嵌套类?

                  相关文章

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

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

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