我的印象是 std::tr1::array 与 boost::array 相同,因为它会在访问越界索引时抛出异常。事实上,我看了一眼标题,看起来也是这样。有人可以解释为什么以下代码会导致总线错误(gcc 版本 4.0.1(Apple Inc. build 5465))和 gcc 4.1.2 上的段错误吗?
谢谢。
#include <exception>
#include <iostream>
#include <string>
#include <tr1/array>
#include <boost/array.hpp>
int main()
{
// boost::array<std::string, 3> arr;
std::tr1::array<std::string, 3> arr;
try
{
arr.at( 0 ) = "one";
arr.at( 1 ) = "two";
arr.at( 2 ) = "three";
arr.at( 3 ) = "nogood";
}
catch ( const std::exception& e )
{
std::cout << "exception: " << e.what() << std::endl;
}
return 0;
}