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


Author: Nikolay Ivchenkov <tsoae@mail.ru>
Date: Fri, 25 Sep 2009 18:30:30 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Florian Weimer <fw@deneb.enyo.de>
Date: Sat, 26 Sep 2009 15:34:27 CST
Raw View
* 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.

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





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Mon, 28 Sep 2009 03:35:38 CST
Raw View
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



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