Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c_str() and data() members of basic_string in C++0x

104 views
Skip to first unread message

Nikolay Ivchenkov

unread,
Sep 25, 2009, 8:30:30 PM9/25/09
to
Paper N2668 defines c_str() and data() members of std::basic_string as
follows:

const charT* c_str() const;
const charT* data() const;

Returns: A pointer to the initial element of an array of length
size() + 1 whose first size() elements equal the corresponding
elements of the string controlled by *this and whose last element is a
null character specified by charT().

Requires: The program shall not alter any of the values stored in
the character array.

This wording is accepted in N2914. I have 4 questions:

1) Shall c_str()/data() return &*begin() when size()>0?
2) Can data() be called concurrently safely?
3) Can c_str() be called concurrently safely?
4) Can these functions throw an exception?

I think, for well-designed string class the answer for the first and
the second questions shall be "yes", the answer for the fourth
question shall be "no", but I don't see such guarantees.

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Florian Weimer

unread,
Sep 26, 2009, 5:34:27 PM9/26/09
to
* Nikolay Ivchenkov:

> This wording is accepted in N2914. I have 4 questions:
>
> 1) Shall c_str()/data() return &*begin() when size()>0?
> 2) Can data() be called concurrently safely?
> 3) Can c_str() be called concurrently safely?
> 4) Can these functions throw an exception?

Historically, the answers are "no" in all cases. It is permissible to
implement using something which is not a plain char array (e.g., ropes).
Calling data() or c_str() needs to build the array, so it can throw
bad_alloc at least.

Daniel Krügler

unread,
Sep 28, 2009, 5:35:38 AM9/28/09
to
On 26 Sep., 02:30, Nikolay Ivchenkov <ts...@mail.ru> wrote:
> Paper N2668 defines c_str() and data() members of std::basic_string as
> follows:
>
> const charT* c_str() const;
> const charT* data() const;
>
> Returns: A pointer to the initial element of an array of length
> size() + 1 whose first size() elements equal the corresponding
> elements of the string controlled by *this and whose last element is a
> null character specified by charT().
>
> Requires: The program shall not alter any of the values stored in
> the character array.
>
> This wording is accepted in N2914. I have 4 questions:
>
> 1) Shall c_str()/data() return &*begin() when size()>0?
> 2) Can data() be called concurrently safely?
> 3) Can c_str() be called concurrently safely?
> 4) Can these functions throw an exception?
>
> I think, for well-designed string class the answer for the first and
> the second questions shall be "yes", the answer for the fourth
> question shall be "no", but I don't see such guarantees.

- Regarding (2) and (3) we have some general statements that
apply to basic_string similarly as to sequence containers e.g.
[container.requirements.dataraces]/2 (combined with [basic.string]/3).

- Regarding (1) and (4) there exists a library issue:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#876

which tries to resolve the missing contiguous-storage requirements,
combined with [string.require]/3 and also suggests stronger exception
safety requirements.

Greetings from Bremen,

Daniel Kr�gler

0 new messages