Title
Inconsistency wrt Allocators in basic_string assignment vs. basic_string::assign
Status
c++17
Section
[string.assign]
Submitter
Marshall Clow

Created on 2016-01-05.00:00:00 last changed 81 months ago

Messages

Date: 2016-02-07.20:24:45

Proposed resolution:

This wording is relative to N4567.

  1. Modify [string.assign] p.1 as indicated:

    basic_string& assign(const basic_string& str);
    

    -1- Effects: Equivalent to *this = strassign(str, 0, npos).

    -2- Returns: *this.

Date: 2016-02-07.20:24:45

[ 2016-02, Issues Telecon ]

P0; move to Tentatively Ready.

Date: 2016-01-05.00:00:00

In issue 2063, we changed the Effects of basic_string::assign(basic_string&&) to match the behavior of basic_string::operator=(basic_string&&), making them consistent.

We did not consider basic_string::assign(const basic_string&), and its Effects differ from operator=(const basic_string&).

Given the following definition:

typedef std::basic_string<char, std::char_traits<char>, MyAllocator<char>> MyString;

MyAllocator<char> alloc1, alloc2;
MyString string1("Alloc1", alloc1);
MyString string2(alloc2);

the following bits of code are not equivalent:

string2 = string1;       // (a) calls operator=(const MyString&)
string2.assign(string1); // (b) calls MyString::assign(const MyString&)

What is the allocator for string2 after each of these calls?

  1. If MyAllocator<char>::propagate_on_container_copy_assignment is true, then it should be alloc2, otherwise it should be alloc1.

  2. alloc2

[string.assign]/1 says that (b) is equivalent to assign(string1, 0, npos), which eventually calls assign(str.data() + pos, rlen). No allocator transfer there.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2016-03-07 04:11:48adminsetstatus: ready -> wp
2016-02-07 20:24:45adminsetmessages: + msg7968
2016-02-07 20:24:45adminsetstatus: new -> ready
2016-01-15 20:48:15adminsetmessages: + msg7676
2016-01-05 00:00:00admincreate