Topic: string::operator[]
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/10/08 Raw View
In article <53bru3$2ls@decaxp.harvard.edu> ccshan@husc.harvard.edu
(Chung-chieh Shan) writes:
|> I'm wondering about the non-const version of string::operator[]
|> (size_t pos) (or rather, basic_string<...>::operator[](size_t pos)).
|> In the April DWP (lib.string.access or 21.1.1.7), this is defined to
|> return data()[pos], but data() itself is defined to return a const
|> char *. No const_cast is present to cast the const char * returned
|> by data() into a char * instead (so that the non-const version of
|> operator[] can return a non-const char &). Am I missing something
|> here, or will this be fixed in a future version?
I think that this is an error. If I understand correctly, there is no
constraint that the characters be store in adjacent memory in the
internal representation of the string.
|> Since operator[] can return a non-const char &, though, I wonder if
|> it is portable to write code that modifies not just the char
|> referenced to by the return value of operator[], but also adjacent
|> chars. For example, if "str" is a string, is it ok to pass &(str[0])
|> to the Unix low-level write() system call (having appropriately
|> resized str before the call)?
It is OK to pass it to write() if and only if the number of characters
read <= 1. As above, I do not believe that there is any constraint that
the characters be stored in contiguous memory. (Note, for example, that
the type of the iterator is implementation defined.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, tudes et r alisations en logiciel orient objet --
-- A la recherche d'une activit dans une region francophone
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: ccshan@husc.harvard.edu (Chung-chieh Shan)
Date: 1996/10/07 Raw View
Hello,
I'm wondering about the non-const version of string::operator[]
(size_t pos) (or rather, basic_string<...>::operator[](size_t pos)).
In the April DWP (lib.string.access or 21.1.1.7), this is defined to
return data()[pos], but data() itself is defined to return a const
char *. No const_cast is present to cast the const char * returned
by data() into a char * instead (so that the non-const version of
operator[] can return a non-const char &). Am I missing something
here, or will this be fixed in a future version?
Since operator[] can return a non-const char &, though, I wonder if
it is portable to write code that modifies not just the char
referenced to by the return value of operator[], but also adjacent
chars. For example, if "str" is a string, is it ok to pass &(str[0])
to the Unix low-level write() system call (having appropriately
resized str before the call)?
Thanks in advance for your help...
--
blue | Chung-chieh Shan (Ken) | Life is a sexually transmitted
() | ccshan@fas.harvard.edu | math too is a terminal representation
/\ | Finger for PGP key | game. disease. of Quality.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]