out of range, no error

I am trying to force an out-of-bounds error and cannot. Can some one please tell me why this is occurring. For the first array-for each combination there is no exception thrown when array items exceed those in std::array. The second produces an error when the initialization list is range-exceeded. Thx in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<array>


int main()
{
	std::array<int, 20>test_array;	//std:: load iteration, then use for - each
	 for (int i = 0; i < 22; ++i)   //NO initialization list -- no error
	 {test_array[i] = 2 * i;}

	 //readback
	 for(auto & element : test_array)
		 std::cout<<element;

	 std::cout<<"||";
	 //typically
	 std::array<int,5>my_array {1,2,3,4,5};   //goes out of range of intialization list, throws error
	 for(auto & elem : my_array)
		std::cout<<elem;

	 return 0;

}
The [] operator does not throw out-of-bounds exceptions. You want the .at method.
Last edited on
"aha"

Thx
Topic archived. No new replies allowed.