This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.

2771. Broken Effects of some basic_string::compare functions in terms of basic_string_view

Section: 23.4.3.8.4 [string.compare] Status: C++17 Submitter: Daniel Krügler Opened: 2016-09-05 Last modified: 2017-07-30

Priority: 1

View all issues with C++17 status.

Discussion:

Some basic_string::compare functions are specified in terms of a non-existing basic_string_view constructor, namely 23.4.3.8.4 [string.compare] p3,

return basic_string_view<charT, traits>(this.data(), pos1, n1).compare(sv);

and 23.4.3.8.4 [string.compare] p4:

return basic_string_view<charT, traits>(this.data(), pos1, n1).compare(sv, pos2, n2);

because there doesn't exist a basic_string_view constructor with three arguments.

Albeit this can be easily fixed by a proper combination of the existing constructor

constexpr basic_string_view(const charT* str, size_type len);

with the additional member function

constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;

it should be decided whether adding the seemingly natural constructor

constexpr basic_string_view(const charT* str, size_type pos, size_type n);

could simplify matters. A counter argument for this addition might be, that basic_string doesn't provide this constructor either.

Another problem is related to the specification of 23.4.3.8.4 [string.compare] p4, which attempts to call a non-existing basic_string_view::compare overload that would match the signature:

constexpr int compare(basic_string_view str, size_type pos1, size_type n1) const;

[2016-09-09 Issues Resolution Telecon]

Marshall to investigate using P/R vs. adding the missing constructor.

[2016-10 Issues Resolution Telecon]

Marshall reports that P/R is better. Status to Tentatively Ready

Proposed resolution:

This wording is relative to N4606.

[Drafting note: The wording changes below are for the same part of the standard as 2758. However, they do not conflict. This one changes the "Effects" of the routine, while the other changes the definition. — end drafting note]

  1. Change 23.4.3.8.4 [string.compare] as indicated:

    int compare(size_type pos1, size_type n1,
                basic_string_view<charT, traits> sv) const;
    

    -3- Effects: Equivalent to:

    return basic_string_view<charT, traits>(this.data(), size()).substr(pos1, n1).compare(sv);
    
    int compare(size_type pos1, size_type n1,
                basic_string_view<charT, traits> sv,
                size_type pos2, size_type n2 = npos) const;
    

    -4- Effects: Equivalent to:

    return basic_string_view<charT, traits>(this.data(), size()).substr(pos1, n1).compare(sv,.substr(pos2, n2));