Topic: behavior of data() in basic_string<...>
Author: glenn@ims.uni-stuttgart.de (Glenn Carroll)
Date: 1996/07/01 Raw View
In reporting a g++ bug, I wrote:
> According to the April '95 draft, basic_string<>::data() should
> return c_str() if size() is non-zero, and a null pointer otherwise.
Cygnus Support replied:
> The draft is wrong.
> --Per Bothner
> Cygnus Support bothner@cygnus.com
Could someone give me a more informative answer? From this remark I
cannot tell if the description in the 4/95 draft was a plain mistake,
if an interim draft has since changed the description, or if GNU/Cygnus
feel that some other Truth overrides the draft.
If an interim draft has indeed changed the description, I'd also be
interested in learning why.
thanks,
glenn
8<==============================================
Glenn Carroll glenn@ims.uni-stuttgart.de
Institut fuer Maschinelle Sprachverarbeitung
Azenbergstr. 12
70174 Stuttgart (49)711-121-1387 office
Germany (49)711-121-1366 fax
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/07/01 Raw View
glenn@ims.uni-stuttgart.de (Glenn Carroll) writes:
>In reporting a g++ bug, I wrote:
>
>> According to the April '95 draft, basic_string<>::data() should
>> return c_str() if size() is non-zero, and a null pointer otherwise.
>
>Cygnus Support replied:
>
>> The draft is wrong.
The latest draft states the following:
| const charT* data() const;
|
| Returns:
| If size() is nonzero, the member returns a pointer to the initial
| element of an array whose first size() elements equal the corre-
| sponding elements of the string controlled by *this. If size() is
| zero, the member returns a non-null pointer that is copyable and can
| have zero added to it.
I imagine that this is so that you can use
`s.data()' and `s.data() + s.size()' as an iterator pair,
without having to treat size() == 0 as a special case.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/07/03 Raw View
In article <4r9a5o$g0a@mulga.cs.mu.OZ.AU> fjh@mundook.cs.mu.OZ.AU
(Fergus Henderson) writes:
|> glenn@ims.uni-stuttgart.de (Glenn Carroll) writes:
|> >In reporting a g++ bug, I wrote:
|> >
|> >> According to the April '95 draft, basic_string<>::data() should
|> >> return c_str() if size() is non-zero, and a null pointer otherwise.
|> >
|> >Cygnus Support replied:
|> >
|> >> The draft is wrong.
|> The latest draft states the following:
|> | const charT* data() const;
|> |
|> | Returns:
|> | If size() is nonzero, the member returns a pointer to the initial
|> | element of an array whose first size() elements equal the corre-
|> | sponding elements of the string controlled by *this. If size() is
|> | zero, the member returns a non-null pointer that is copyable and can
|> | have zero added to it.
|> I imagine that this is so that you can use
|> `s.data()' and `s.data() + s.size()' as an iterator pair,
|> without having to treat size() == 0 as a special case.
But you can do this anyway (I think). Adding 0 to the null pointer
still results in the null pointer. (I say I think, because I'm not
really sure if this is guaranteed, or simply an artifact of any
reasonable implementation. The only constraints on addition are that
"one operand shall be a pointer to a completely defined object type..."
The word "type" would seem to imply that the constraint here only
involves the type.)
Of course, if you want the iterator, you should use the functions begin
and end anyway.
Also, just curious, but can there exist a non-null pointer which is
copyable, but cannot have zero added to it. To be copyable, a pointer
must be valid, e.g.: either null or point to a valid object. Or am I
missing something here?
--
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, itudes et rialisations en logiciel orienti objet --
-- A la recherche d'une activiti dans une region francophone
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/07/03 Raw View
kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:
>fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) writes:
>
>|> | const charT* data() const;
>|> | [...] If size() is
>|> | zero, the member returns a non-null pointer that is copyable and can
>|> | have zero added to it.
>
>|> I imagine that this is so that you can use
>|> `s.data()' and `s.data() + s.size()' as an iterator pair,
>|> without having to treat size() == 0 as a special case.
>
>But you can do this anyway (I think). Adding 0 to the null pointer
>still results in the null pointer. (I say I think, because I'm not
>really sure if this is guaranteed, or simply an artifact of any
>reasonable implementation. The only constraints on addition are that
>"one operand shall be a pointer to a completely defined object type..."
>The word "type" would seem to imply that the constraint here only
>involves the type.)
You are right that adding 0 to the null pointer still results in the
null pointer. However, that is a quite recent change; it's not
guaranteed in C, and until recently it wasn't guaranteed in C++.
The change in the definition of string::data() may date from before
that change.
>Of course, if you want the iterator, you should use the functions begin
>and end anyway.
Yes. I suppose my guess at the rationale quoted above is not correct.
An alternative possible rationale is efficiency: a string implementation
that doesn't treat zero-length strings specially internally would
be able to implement data() fast if it didn't have to check for size() == 0
and handle it as a special case by returning a null pointer.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]