This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Resolved status.
basic_string
constructorSection: 27.4.3.3 [string.cons] Status: Resolved Submitter: Andrzej Krzemienski Opened: 2018-05-09 Last modified: 2020-09-06
Priority: 2
View all other issues in [string.cons].
View all issues with Resolved status.
Discussion:
The following is the spec for basic_string
constructor taking a pointer and a size in
N4741 ([string.cons]/12-14):
basic_string(const charT* s, size_type n, const Allocator& a = Allocator());
(12) Requires:
s
points to an array of at leastn
elements ofcharT
.(13) Effects: Constructs an object of class
basic_string
and determines its initial string value from the array ofcharT
of lengthn
whose first element is designated bys
.(14) Postconditions:
data()
points at the first element of an allocated copy of the array whose first element is pointed at bys
,size()
is equal ton
, andcapacity()
is a value at least as large assize()
.
This implies that passing a null pointer and a zero size to this constructor is violating the precondition, as null
pointer cannot be described as "pointing to an array of at least n
elements of charT
".
On the other hand, being able to pass {nullptr, 0}
is essential for basic_string
to be
able to inter-operate with other containers that are allowed to use the null pointer value to represent sequences
of size zero:
std::vector<char> v{}; assert(v.data() == nullptr); // on some implementations std::string s(v.data(), v.size()); // nullptr on some implementations
This has been already acknowledged as a defect in issue 2235(i) and applied, but the resolution still implies a too strong precondition.
Previous resolution [SUPERSEDED]:
This wording is relative to N4741.
Edit 27.4.3.3 [string.cons] as indicated:
basic_string(const charT* s, size_type n, const Allocator& a = Allocator());-12- Requires: Unless
-13- Effects: Constructs an object of classn == 0
,s
points to an array of at leastn
elements ofcharT
.basic_string
and, unlessn == 0
, determines its initial string value from the array ofcharT
of lengthn
whose first element is designated bys
. -14- Postconditions: Ifn != 0
, thendata()
points at the first element of an allocated copy of the array whose first element is pointed at bys
,;size()
is equal ton
, andcapacity()
is a value at least as large assize()
.
[2016-06-04 Marshall provides alternate resolution]
[2018-06-18 after reflector discussion]
Priority set to 2
[2018-08 mailing list discussion]
This will be resolved by Tim's string rework paper.
Resolved by the adoption of P1148 in San Diego.
Proposed resolution:
This wording is relative to N4750.
Edit 27.4.3.3 [string.cons] as indicated:
basic_string(const charT* s, size_type n, const Allocator& a = Allocator());-12- Requires:
[s, s + n)
is a valid range.s
points to an array of at leastn
elements ofcharT
-13- Effects: Constructs an object of class
-14- Postconditions:basic_string
and determines its initial string value from the range[s, s + n)
array of.charT
of lengthn
whose first element is designated bys
data()
points at the first element of an allocated copy of the array whose first element is pointed at bys
,size()
is equal ton
,andcapacity()
is a value at least as large assize()
, andtraits::compare(data(), s, n) == 0
.