Title
String ctors specify wrong default allocator
Status
tc1
Section
[basic.string]
Submitter
Nathan Myers

Created on 1998-08-06.00:00:00 last changed 171 months ago

Messages

Date: 2010-10-21.18:28:33

[ Kona: issue editing snafu fixed - the proposed resolution now correctly reflects the LWG consensus. ]

Date: 2010-10-21.18:28:33

Rationale:

The LWG believes the constructor is actually broken, rather than just an unfortunate design choice.

The LWG considered two other possible resolutions:

A. In [basic.string], replace the declaration of the copy constructor as follows:

basic_string(const basic_string& str, size_type pos = 0,
             size_type n = npos);
basic_string(const basic_string& str, size_type pos,
             size_type n, const Allocator& a); 

In [string.require], replace the copy constructor declaration as above. Add to paragraph 5, Effects:

When no Allocator argument is provided, the string is constructed using the value str.get_allocator().

B. In [basic.string], and also in [string.require], replace the declaration of the copy constructor as follows:

basic_string(const basic_string& str, size_type pos = 0,
             size_type n = npos); 

The proposed resolution reflects the original intent of the LWG. It was also noted by Pete Becker that this fix "will cause a small amount of existing code to now work correctly."

Date: 2010-10-21.18:28:33

Proposed resolution:

In [basic.string], replace the declaration of the copy constructor as follows:

basic_string(const basic_string& str);
basic_string(const basic_string& str, size_type pos, size_type n = npos,
             const Allocator& a = Allocator());

In [string.require], replace the copy constructor declaration as above. Add to paragraph 5, Effects:

In the first form, the Allocator value used is copied from str.get_allocator().

Date: 1998-08-06.00:00:00

The basic_string<> copy constructor:

basic_string(const basic_string& str, size_type pos = 0,
             size_type n = npos, const Allocator& a = Allocator()); 

specifies an Allocator argument default value that is counter-intuitive. The natural choice for a the allocator to copy from is str.get_allocator(). Though this cannot be expressed in default-argument notation, overloading suffices.

Alternatively, the other containers in Clause 23 (deque, list, vector) do not have this form of constructor, so it is inconsistent, and an evident source of confusion, for basic_string<> to have it, so it might better be removed.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg97
2010-10-21 18:28:33adminsetmessages: + msg96
2010-10-21 18:28:33adminsetmessages: + msg95
1998-08-06 00:00:00admincreate