index check in std::array
Gabriel Dos Reis
gdr@integrable-solutions.net
Tue Oct 30 10:04:00 GMT 2012
On Tue, Oct 30, 2012 at 1:52 AM, Michael Buro <michael.buro@gmail.com> 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.
>
> 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
Techically, you would get an ODR violation unless
you set NDEBUG consistently in all translation units.
Use at().
-- Gaby
More information about the Libstdc++
mailing list