[patch] std::string::operator[] extension in debug mode
Jonathan Wakely
cow@compsoc.man.ac.uk
Thu May 19 10:25:00 GMT 2005
On Thu, May 19, 2005 at 01:10:11AM +0200, Paolo Carlini wrote:
> Jonathan Wakely wrote:
>
> >OK for mainline and 4.0 ?
> >
> >
> Excellent, OK!
Committed.
Next issue is that those assertions are never triggered outside of the
testsuite, because with _GLIBCXX_EXTERN_TEMPLATE the std::string
instantiation is in libstdc++.so, which is not compiled with
_GLIBCXX_DEBUG and so doesn't contain the assertions.
Basically, this doesn't assert:
#define _GLIBCXX_DEBUG
#include <string>
int main()
{
std::string s;
s[1];
}
How does the testsuite avoid the extern template instantiations?
I need to change debug.html to document this behaviour and to describe how
you can get the assertions. Presumably the best way is to configure with
--with-libstdcxx-debug-flags="... -D_GLIBCXX_DEBUG" and to link to the
debug/libstdc++.so lib? Or am I misunderstanding something?
Finally (for now), something like the attached patch is needed to make
__gnu_debug::basic_string behave the same way (and to add the missing
default template-arguments and string/wstring typedefs). I will work on
this a bit more though, that type is never used in place of std::string,
you have to refer to it explicitly as __gnu_debug::basic_string so it's
not urgent.
I need to fix debug.html to make that clear as well, it implies that
defining _GLIBCXX_DEBUG will use that template, as with
std::vector/__gnu_debug::vector etc. but that's not true for
std::string.
(See why I said I got distracted?)
jon
--
"It has always been the prerogative of children and half-wits to point out
that the emperor has no clothes. But the half-wit remains a half-wit, and
the emperor remains an emperor."
- Neil Gaiman
-------------- next part --------------
Index: include/debug/string
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/debug/string,v
retrieving revision 1.2
diff -u -p -b -B -r1.2 string
--- include/debug/string 9 Dec 2003 04:26:28 -0000 1.2
+++ include/debug/string 19 May 2005 09:53:35 -0000
@@ -1,6 +1,6 @@
// Debugging string implementation -*- C++ -*-
-// Copyright (C) 2003
+// Copyright (C) 2003, 2005
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
@@ -37,7 +37,8 @@
namespace __gnu_debug
{
- template<typename _CharT, typename _Traits, typename _Allocator>
+template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
+ typename _Allocator = std::allocator<_CharT> >
class basic_string
: public std::basic_string<_CharT, _Traits, _Allocator>,
public __gnu_debug::_Safe_sequence<basic_string<_CharT, _Traits,
@@ -213,7 +214,16 @@ namespace __gnu_debug
reference
operator[](size_type __pos)
{
+#ifdef _GLIBCXX_DEBUG_PEDANTIC
__glibcxx_check_subscript(__pos);
+#else
+ // as an extension v3 allows s[s.size()] when s is non-const.
+ _GLIBCXX_DEBUG_VERIFY(__pos <= this->size(),
+ _M_message(::__gnu_debug::__msg_subscript_oob)
+ ._M_sequence(*this, "this")
+ ._M_integer(__pos, "__pos")
+ ._M_integer(this->size(), "size"));
+#endif
return _M_base()[__pos];
}
@@ -996,6 +1006,13 @@ namespace __gnu_debug
__str._M_invalidate_all();
return __res;
}
+
+ typedef basic_string<char> string;
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+ typedef basic_string<wchar_t> wstring;
+#endif
+
} // namespace __gnu_debug
#endif
More information about the Libstdc++
mailing list