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

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

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

    1. <tfoot id='UAdZG'></tfoot>

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

    2. std::vector 的编译时触发范围检查

      时间:2023-09-16
      <tfoot id='EPJx9'></tfoot>
          <tbody id='EPJx9'></tbody>

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

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

          • <legend id='EPJx9'><style id='EPJx9'><dir id='EPJx9'><q id='EPJx9'></q></dir></style></legend>
            • <bdo id='EPJx9'></bdo><ul id='EPJx9'></ul>
                本文介绍了std::vector 的编译时触发范围检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                目标:

                我想要一个范围检查版本的 std::vectoroperator [] 用于我的调试版本,并且在发布模式下没有范围检查.

                调试模式下的范围检查显然有利于调试,但它会导致我希望避免的发布代码速度降低 5% - 10%.

                可能的解决方案:

                我在 Stroustrup 的C++ 编程语言"中找到了解决方案.他做了以下事情:

                template 类checked_vector : public std::vector<T>{上市:使用 std::vector::vector;//用at()覆盖操作符[]};

                这是有问题的,因为它继承自具有危险的非虚拟析构函数的类.(还有休息室 不太喜欢 喜欢那个解决方案.)

                另一个想法是这样的类:

                template 类checked_vector {std::vector数据_;上市://手工把所有 std::vector 的公共方法放在这里};

                这既乏味又会产生大量的复制粘贴,这也很糟糕.

                上述两种解决方案的好处是我可以简单地使用我的 makefile 中的宏定义来打开和关闭它们.

                问题:

                1. 有更好的解决方案吗?(如果没有,为什么不呢?)
                2. 如果不是,上述其中一项是否可以接受?(我知道这是基于意见的,如果可能,请关注第一.)

                解决方案

                如果我没记错的话,这是 Visual Studio 的常见情况.使用 g++,您必须使用 -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC 调用编译器.(很可能你三个都不需要,但我都用了三系统.)与其他编译器,检查文​​档.这里标准中未定义行为的目的正是为了允许这种事情.

                The goal:

                I would like to have a range checked version of std::vector's operator [] for my debug builds and no range check in release mode.

                The range check in debug mode is obviously good for debugging, but it causes a slowdown of 5% - 10% in my release code which I would like to avoid.

                Possible solutions:

                I found a solution in Stroustrup's "The C++ programming language". He did the following:

                template <class T>
                class checked_vector : public std::vector<T> {
                    public:
                        using std::vector<T>::vector;
                
                        //override operator [] with at()
                };
                

                This is problematic because it inherits from a class with non-virtual destructor which is dangerous. (And the Lounge was not too fond of that solution.)

                Another idea would be a class like this:

                template <class T>
                class checked_vector {
                    std::vector<T> data_;
                
                    public:
                        //put all public methods of std::vector here by hand
                
                };
                

                This would be both tedious and create a large amount of copy-paste which is bad too.

                The nice thing about both the above solutions is that I can simply toggle them on and off with a macro definition in my makefile.

                The questions:

                1. Is there a better solution? (If not, why not?)
                2. If not, is one of the above considered acceptable? (I know this one is opinion based, please focus on No. 1 if possible.)

                解决方案

                If I'm not mistaken, this is the usual situation with Visual Studio. With g++, you have to invoke the compiler with -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC. (It's probable that you don't need all three, but I use all three systematically.) With other compilers, check the documentation. The purpose of the undefined behavior in the standard here is precisely to allow this sort of thing.

                这篇关于std::vector 的编译时触发范围检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我如何使用 std::vector&lt;std::mutex&gt; 之类的 下一篇:从字符串中获取 IPv4 地址的最快方法

                相关文章

                最新文章

                <tfoot id='umFDi'></tfoot>

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

                    <bdo id='umFDi'></bdo><ul id='umFDi'></ul>
                1. <legend id='umFDi'><style id='umFDi'><dir id='umFDi'><q id='umFDi'></q></dir></style></legend>

                  1. <small id='umFDi'></small><noframes id='umFDi'>