为什么C ++检测超出范围向量元素是否被默认使用[]运算符? [英] Why doesn't C++ detect when out of range vector elements are accessed with the [ ] operators by default?

查看:199
本文介绍了为什么C ++检测超出范围向量元素是否被默认使用[]运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



编辑:下面是一个例子在伪代码(我相信 - 纠正我, operator []:

  Item_Type& operator [](size_t index){//验证索引是否合法。 
if(index< 0 || index> = num_items){
throw std :: out_of_range
(对操作符[]的索引超出范围
}
return the_data [index]
}



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

解决方案

] ,边界检查会增加显着的开销。



考虑

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

此函数转换为只有三行程序集

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

使用 at()的边界检查版本

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

 <$ cd $ c 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 *,...)
/ pre>

即使在正常(非抛出)代码路径中,这是8行汇编 - 几乎是原来的三倍。


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++?

EDIT: 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.

Consider

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

this function translates to just three lines of assembly:

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

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

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

This becomes

    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*, ...)

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

这篇关于为什么C ++检测超出范围向量元素是否被默认使用[]运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆