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

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

    <tfoot id='C7NuY'></tfoot>
    <legend id='C7NuY'><style id='C7NuY'><dir id='C7NuY'><q id='C7NuY'></q></dir></style></legend>

      1. 默认情况下,为什么 C++ 不检测何时使用 [] 运算

        时间:2023-09-15
      2. <tfoot id='YnoKh'></tfoot>
      3. <small id='YnoKh'></small><noframes id='YnoKh'>

              <tbody id='YnoKh'></tbody>

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

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

                  <i id='YnoKh'><tr id='YnoKh'><dt id='YnoKh'><q id='YnoKh'><span id='YnoKh'><b id='YnoKh'><form id='YnoKh'><ins id='YnoKh'></ins><ul id='YnoKh'></ul><sub id='YnoKh'></sub></form><legend id='YnoKh'></legend><bdo id='YnoKh'><pre id='YnoKh'><center id='YnoKh'></center></pre></bdo></b><th id='YnoKh'></th></span></q></dt></tr></i><div id='YnoKh'><tfoot id='YnoKh'></tfoot><dl id='YnoKh'><fieldset id='YnoKh'></fieldset></dl></div>
                • 本文介绍了默认情况下,为什么 C++ 不检测何时使用 [] 运算符访问超出范围的向量元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道数组是一个原始类,因此没有内置方法来检测超出范围的错误.但是,vector 类具有内置函数 .at() 可以检测这些错误.通过使用命名空间,任何人都可以通过在访问向量范围之外的值时抛出错误来重载 [ ] 符号以充当 .at() 函数.我的问题是:为什么这个功能在 C++ 中不是默认的?

                  I understand that arrays are a primitive class and therefore do not have built in methods to detect out of range errors. However, the vector class has the built in function .at() which does detect these errors. By using namespaces, anyone can overload the [ ] symbols to act as the .at() function by throwing an error when a value out of the vector's range is accessed. My question is this: why is this functionality not default in C++?

                  下面是重载向量运算符 [ ] 的伪代码示例(我相信 - 如果需要请纠正我):

                  Below is an example in pseudocode (I believe - correct me if needed) of overloading the vector operator [ ]:

                  Item_Type& operator[](size_t index) { // Verify that the index is legal.
                  if (index < 0 || index >= num_items) {
                     throw std::out_of_range
                       ("index to operator[] is out of range");
                  }
                   return the_data[index]
                  }
                  

                  我相信这个函数可以写入用户定义的命名空间并且相当容易实现.如果这是真的,为什么它不是默认的?

                  I believe this function can be written into a user-defined namespace and is reasonably easy to implement. If this is true, why is it not default?

                  推荐答案

                  对于通常与 [] 一样便宜的东西,边界检查会增加大量开销.

                  For something that's normally as cheap as [], bounds checking adds a significant overhead.

                  考虑

                  int f1(const std::vector<int> & v, std:size_t s) { return v[s]; }
                  

                  这个函数转化为只有三行汇编:

                      movq    (%rdi), %rax
                      movl    (%rax,%rsi,4), %eax
                      ret
                  

                  现在考虑使用 at() 的边界检查版本:

                  Now consider the bounds-checking version using at():

                  int f2(const std::vector<int> & v, std:size_t s) { return v.at(s); }
                  

                  这就变成了

                      movq    (%rdi), %rax
                      movq    8(%rdi), %rdx
                      subq    %rax, %rdx
                      sarq    $2, %rdx
                      cmpq    %rdx, %rsi
                      jae .L6
                      movl    (%rax,%rsi,4), %eax
                      ret
                  .L6:
                      pushq   %rax
                      movl    $.LC1, %edi
                      xorl    %eax, %eax
                      call    std::__throw_out_of_range_fmt(char const*, ...)
                  

                  即使在正常(非抛出)代码路径中,也有 8 行汇编代码 - 几乎是其三倍.

                  Even in the normal (non-throwing) code path, that's 8 lines of assembly - almost three times as many.

                  这篇关于默认情况下,为什么 C++ 不检测何时使用 [] 运算符访问超出范围的向量元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++向量&lt;vector&lt;double&gt;&gt;加倍** 下一篇:向量将所有负值转换为零

                  相关文章

                  最新文章

                  <legend id='QjEpE'><style id='QjEpE'><dir id='QjEpE'><q id='QjEpE'></q></dir></style></legend>
                    <bdo id='QjEpE'></bdo><ul id='QjEpE'></ul>
                • <tfoot id='QjEpE'></tfoot>

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

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