index check in std::array

Jonathan Wakely jwakely.gcc@gmail.com
Tue Oct 30 10:18:00 GMT 2012


On 30 October 2012 06:52, Michael Buro wrote:
>
> 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.

It's not fixed in 4.7.2 because that version is already released and
it's not an important enough change to make in a stable release
branch.

You can follow the progress at
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51850 which says it's
scheduled for 4.8.0 (though that could change)

> The task can be accomplished quite easily - as shown in several forum
> threads. E.g.
[snip]
>       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];
>       }

Not so easily, that is not a valid constexpr function.

Anyway libstdc++ has its own debug mode, it doesn't depend on NDEBUG.



More information about the Libstdc++ mailing list