This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
index check in std::array
- From: Michael Buro <michael dot buro at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Tue, 30 Oct 2012 00:52:22 -0600
- Subject: index check in std::array
- References: <1351579343.11385.ezmlm@gcc.gnu.org>
- Reply-to: michael dot buro at gmail dot com
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