http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58163
Bug ID: 58163
Summary: [C++11] Pedantic assert on str[str.size()] is wrong in
C++11
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: ppluzhnikov at google dot com
Google ref b/10322506
/// --- cut ---
#include <string>
int main()
{
const std::string cs;
std::string s;
if (cs[0] != '\0') return 1;
if (s[0] != '\0') return 2;
return 0;
}
/// --- cut ---
Using trunk gcc g++ (GCC) 4.9.0 20130814 (experimental)
g++ -g t.cc -D_GLIBCXX_DEBUG_PEDANTIC -D_GLIBCXX_DEBUG -std=c++11 && ./a.out
/gcc-svn-install/include/c++/4.9.0/bits/basic_string.h:848:
std::basic_string<_CharT, _Traits, _Alloc>::reference std::basic_string<_CharT,
_Traits, _Alloc>::operator[](std::basic_string<_CharT, _Traits,
_Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits,
_Alloc>::reference = char&; std::basic_string<_CharT, _Traits,
_Alloc>::size_type = long unsigned int]: Assertion '__pos < size()' failed.
In C++98:
21.3.4 basic_string element access [lib.string.access]
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Returns: If pos < size(), returns data()[pos]. Otherwise, if pos == size(),
the const version returns charT().
Otherwise, the behavior is undefined. <<== PEDASSERT catches this.
However, in C++11:
21.4.5 basic_string element access [string.access]
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Requires: pos <= size().
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object
of type T with value charT(); the referenced value shall not be modied.
Note removal of undefined behavior.