krishanu.debnath@gmail.com skrev:
[color=blue]
>
peter.koch.larsen@gmail.com wrote:[color=green]
> >
krishanu.debnath@gmail.com skrev:
> >[/color]
>
> <snip>
>[color=green][color=darkred]
> > > >
> > > > std::string s("Hello");
> > > > if (s[5] == '\0') ...
> > > >
> > > > This last line is undefined behaviour although i would not be surprised
> > > > if the comparison was true.
> > >
> > > Why do you think last line invokes UB? Sec 21.3.4 clearly says that
> > > behavior
> > > is undefined only if index is greater than size().[/color]
> >
> > Well - from my reading (a preliminary version on the net, but I doubt
> > that changes have been made in this area) it is clear to me that index
> > must be smaller than size() (or length() if you prefer) unless the[/color]
>
> We can only justify the behavior by whats written in standard, not by
> any
> material you see on net.[/color]
Well... most of the draft standard on the net is pretty accurate. But
let me quote the standard quote from one of your other posts in this
thread:
<begin quote>
21.3.4 - basic_string element access [lib.string.access]
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
-1- Returns: If pos < size(), returns data()[ pos ]. Otherwise, if pos
== size(), the const version returns charT(). Otherwise, the behavior
is undefined.
<end quote>
This part is word by word the same as the draft I read. Clearly both
quotes support my view:
[color=blue][color=green][color=darkred]
> > > >
> > > > std::string s("Hello");
> > > > if (s[5] == '\0') ...
> > > >
> > > > This last line is undefined behaviour although i would not be surprised
> > > > if the comparison was true.
> > >[/color][/color][/color]
[color=blue]
>
> Krishanu[/color]
/Peter