fork download
  1. #include <bits/stdc++.h>
  2. #define elem(object,index) object[bounds_check(index,object.size(),__LINE__)]
  3.  
  4. using namespace std;
  5.  
  6. inline int bounds_check(int index, int size, int line) {
  7. if (index < 0 or index >= size)
  8. throw out_of_range("At line "+to_string(line)+": element index "+to_string(index)+
  9. " is out-of-range [0,"+to_string(size-1)+']');
  10. return index; }
  11.  
  12. int main() {
  13.  
  14. int size, index, value;
  15.  
  16. cin >> size >> index >> value;
  17.  
  18. vector<int> x(size);
  19.  
  20. elem(x,index) = value;
  21.  
  22. for (int index = 0; index < size; ++index)
  23. cout << "x[" << index << "] = " << x[index] << endl;
  24. }
  25.  
Runtime error #stdin #stdout #stderr 0s 4352KB
stdin
9 10 1
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::out_of_range'
  what():  At line 20: element index 10 is out-of-range [0,8]