Topic: std::string" question
Author: "Stephen Howe" <SPAMstephen.howeGUARD@tnsofres.com>
Date: Thu, 18 Apr 2002 07:26:16 GMT Raw View
"Lyle Johnson" <nospam@nospam.com> wrote in message
news:cHiv8.1555$8Xd.96@news02.bloor.is.net.cable.rogers.com...
> Is it legal to extract the last character + 1 from a std::string? Like so:
>
> std::string Somestring = "Test";
> char LastPlusOne = Somestring[SomeString.size()]; // Legal?
No, definitely not, so don't do it.
> Is this legal according to the standard. Since "std::string" isn't
> technically a traditional C string under the hood, can I expect a NULL
> terminator here.
No you cannot, there is no guarantee whatsoever that anything is there.
Even if you find that your implementation lets you get away with it and it
appears to be '\0', don't do it.
Stephen Howe
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: James Kanze <kanze@gabi-soft.de>
Date: Thu, 18 Apr 2002 16:20:15 GMT Raw View
"Stephen Howe" <SPAMstephen.howeGUARD@tnsofres.com> writes:
|> "Lyle Johnson" <nospam@nospam.com> wrote in message
|> news:cHiv8.1555$8Xd.96@news02.bloor.is.net.cable.rogers.com...
|> > Is it legal to extract the last character + 1 from a std::string?
|> > Like so:
|> > std::string Somestring =3D "Test";
|> > char LastPlusOne =3D Somestring[SomeString.size()]; // Legal?
|> No, definitely not, so don't do it.
On the other hand, it is legal if the string is const. (Don't ask me
why this anomaly.)
|> > Is this legal according to the standard. Since "std::string" isn't
|> > technically a traditional C string under the hood, can I expect a
|> > NULL terminator here.
|> No you cannot, there is no guarantee whatsoever that anything is
|> there. Even if you find that your implementation lets you get away
|> with it and it appears to be '\0', don't do it.
The standard says that it is defined for a const string.
--=20
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Stephen Howe" <SPAMstephen.howeGUARD@tnsofres.com>
Date: Thu, 18 Apr 2002 17:45:33 GMT Raw View
|> No, definitely not, so don't do it.
On the other hand, it is legal if the string is const. (Don't ask me
why this anomaly.)
Ok, point noted. Thanks for this, I did not know.
Stephen Howe
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]