Re: CString help
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Mon, 25 Jun 2007 05:57:59 -0500
On Mon, 25 Jun 2007 00:26:54 -0700, "Mihai N." <nmihai_year_2000@xxxxxxxxx>
wrote:
The following will be fine:
string s;
s.resize(n);
memset(&s[0], 0, n);
One of the benefits to requiring s[0] to return a real reference (which is
what disallows COW) is that &s[0] returns a real pointer.
From wat I know works in all current implementations, but still not*guaranteed* by the standard in any way.
This is going 'round in circles. :) You've restated what I said at the
beginning:
<q>
Practically speaking, you can do this:
const int N = 100;
std::string s;
s.resize(N);
int len = WindowsAPI(&s[0], N);
s.resize(len);
Though not guaranteed, I don't know any implementation where this won't
work. (I'd really like the non-contiguity, reference-counted, etc options
to just go away. People who need those things can't do without them, and
the possibility they exist complicates things for everyone else.)
</q>
I later posted a link to this proposal, whose adoption guarantees this
currently univerally supported behavior:
530. Must elements of a string be contiguous?
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530
In my previous message, I was responding to your comment about "After
reading your open-std.org link..." to give you an example of the sort of
things it makes kosher. I could have posted my earlier example instead,
because it's the same thing. Besides completing the circle, your latest
reply is a great example of what I meant by, "the possibility they exist
complicates things for everyone else." Even after I started by saying it's
non-standard, though TTBOMK universally supported, then posted a link to a
document that indicates it will become standardized, you came back with,
"it's not standard". Like I said, going 'round in circles. :)
Bottom line: Unless you want to reserve the possibility of replacing
std::string with flex_string or whatever, you should feel free to go ahead
and use the technique I presented, which comes in really handy when dealing
with Windows APIs. You should even feel good about it. :)
--
Doug Harrison
Visual C++ MVP
.
- Follow-Ups:
- Re: CString help
- From: David Wilkinson
- Re: CString help
- From: Mihai N.
- Re: CString help
- References:
- Re: CString help
- From: jp2code
- Re: CString help
- From: David Wilkinson
- Re: CString help
- From: Mihai N.
- Re: CString help
- From: Doug Harrison [MVP]
- Re: CString help
- From: Mihai N.
- Re: CString help
- From: Doug Harrison [MVP]
- Re: CString help
- From: Mihai N.
- Re: CString help
- From: Doug Harrison [MVP]
- Re: CString help
- From: Mihai N.
- Re: CString help
- From: Doug Harrison [MVP]
- Re: CString help
- From: Mihai N.
- Re: CString help
- Prev by Date: Re: App Unicode call MBCS dll
- Next by Date: Re: App Unicode call MBCS dll
- Previous by thread: Re: CString help
- Next by thread: Re: CString help
- Index(es):
Relevant Pages
|