index check in std::array

Michael Buro michael.buro@gmail.com
Tue Oct 30 06:52:00 GMT 2012


I am wondering why in g++ 4.7.2 (and possibly later versions) indexes
still are not checked in std::array operator[] when compiling in debug
mode, which is IMHO the biggest advantage std::array has over
C-arrays. boost::array has this feature.

The task can be accomplished quite easily - as shown in several forum
threads. E.g.

      // Element access.
      reference
      operator[](size_type __n)
      {
// added
#ifndef NDEBUG
        if (__n >= _Nm)
          std::__throw_out_of_range(__N("array::[]"));
#endif        
        return _M_instance[__n];
      }

      constexpr const_reference
      operator[](size_type __n) const noexcept
      {
// added
#ifndef NDEBUG
        if (__n >= _Nm)
          std::__throw_out_of_range(__N("array::[]"));
#endif        
        return _M_instance[__n];
      }

/MB



More information about the Libstdc++ mailing list