[patch] std::string::operator[] extension in debug mode

Jonathan Wakely cow@compsoc.man.ac.uk
Wed May 18 23:07:00 GMT 2005


As discussed this makes debug mode support the v3 extension allowing
s[s.size()] on non-const strings.  This fixes these FAILs when the tests
are run in debug mode:
FAIL: 21_strings/basic_string/element_access/char/empty.cc execution test
FAIL: 21_strings/basic_string/element_access/wchar_t/empty.cc execution test

I decided to order the assertions this way so that in non-pedantic mode
pos > size() will produce an error message that shows the extension
would work.

2005-05-19  Jonathan Wakely  <...>

	* include/bits/basic_string.h (operator[]): Allow s[s.size()] in
        debug mode, but not pedantic mode.

OK for mainline and 4.0 ?

jon

-------------- next part --------------
Index: include/bits/basic_string.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/basic_string.h,v
retrieving revision 1.75
diff -u -p -r1.75 basic_string.h
--- include/bits/basic_string.h	25 Apr 2005 10:59:21 -0000	1.75
+++ include/bits/basic_string.h	18 May 2005 22:49:22 -0000
@@ -695,7 +695,10 @@ namespace std
       reference
       operator[](size_type __pos)
       {
-	_GLIBCXX_DEBUG_ASSERT(__pos < size());
+        // allow pos == size() as v3 extension:
+	_GLIBCXX_DEBUG_ASSERT(__pos <= size());
+        // but be strict in pedantic mode:
+	_GLIBCXX_DEBUG_PEDASSERT(__pos < size());
 	_M_leak();
 	return _M_data()[__pos];
       }


More information about the Libstdc++ mailing list