2757. std::string{}.insert(3, "ABCDE", 0, 1) is ambiguous

Section: 27.4.3.7.4 [string.insert] Status: Resolved Submitter: Marshall Clow Opened: 2016-07-30 Last modified: 2020-09-06

Priority: 1

View all other issues in [string.insert].

View all issues with Resolved status.

Discussion:

Before C++17, we had the following signature to std::basic_string:

basic_string&
  insert(size_type pos1, const basic_string& str, size_type pos2, size_type n = npos);

Unlike most of the other member functions on std::basic_string, there were not corresponding versions that take a charT* or (charT *, size).

In p0254r2, we added:

basic_string&
  insert(size_type pos1, basic_string_view<charT, traits> sv, size_type pos2, size_type n = npos);

which made the code above ambiguous. There are two conversions from "const charT*", one to basic_string, and the other to basic_string_view, and they're both equally good (in the view of the compiler).

This ambiguity also occurs with the calls

assign(const basic_string& str,             size_type pos, size_type n = npos);
assign(basic_string_view<charT, traits> sv, size_type pos, size_type n = npos);

but I will file a separate issue (2758) for that.

A solution is to add even more overloads to insert, to make it match all the other member functions of basic_string, which come in fours (string, pointer, pointer + size, string_view).

[2016-08-03, Chicago, Robert Douglas provides wording]

Previous resolution [SUPERSEDED]:

This wording is relative to N4606.

  1. In 27.4.3 [basic.string] modify the synopsis for basic_string as follows:

    namespace std {
      template<class charT, class traits = char_traits<charT>,
        class Allocator = allocator<charT>>
      class basic_string {
      public:
        […]
        template<class T>
        basic_string& insert(size_type pos1, basic_string_view<charT, traits>T sv,
                             size_type pos2, size_type n = npos);
        […]
      };
    }
    
  2. In 27.4.3.7.4 [string.insert], modify basic_string_view overload as follows:

    template<class T>
    basic_string& insert(size_type pos1, basic_string_view<charT, traits>T sv,
                         size_type pos2, size_type n = npos);
    

    […]

    -?- Remarks: This function shall not participate in overload resolution unless is_same_v<T, basic_string_view<charT, traits>> is true.

[2016-08-04, Chicago, Robert Douglas comments]

For the sake of simplicity, the previous wording suggestion has been merged into the proposed wording of LWG 2758.

[08-2016, Chicago]

Fri PM: Move to Tentatively Ready (along with 2758).

[2016-09-09 Issues Resolution Telecon]

Since 2758 has been moved back to Open, move this one, too

[2016-10 Telecon]

Ville's wording for 2758 has been implemented in libstdc++ and libc++. Move 2758 to Tentatively Ready and this one to Tentatively Resolved

Proposed resolution:

This issue is resolved by the proposed wording for LWG 2758.