Visual C++:checked_array_iterator如何有用?

Ash*_*ppa 6 warnings iterator visual-c++

警告级别4(/ W4)编译代码时,我在参数为C数组的调用(而不是像向量的STL容器)上收到C4996警告std::copy().修复此问题的推荐解决方案似乎是使用stdext :: checked_array_iterator.

有什么用的stdext :: checked_array_iterator?它是如何工作的?

为什么它不会对在Visual C++ 2010下编译的这段错误代码发出任何编译警告?:

#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    int arr0[5] = {100, 99, 98, 97, 96};
    int arr1[3];
    copy( arr0, arr0 + 5, stdext::checked_array_iterator<int*>( arr1, 3 ) );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Han*_*son 3

这个页面,检查迭代器,描述了它是如何工作的,但是这句话总结了它:检查迭代器确保您不会覆盖容器的边界。

因此,如果超出迭代器的范围,它将抛出异常或调用invalid_parameter