https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127918

            Bug ID: 127918
           Summary: ranges::mismatch fails to use borrowed_iterator_t
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jesseconnell at gmail dot com
  Target Milestone: ---

Created attachment 65858
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65858&action=edit
Patch with proposed fix

The range overload of std::ranges::mismatch uses iterator_t instead of
borrowed_iterator_t for both result members, so non-borrowed rvalue ranges
produce iterators instead of ranges::dangling.

#include <algorithm>
#include <concepts>
#include <ranges>
#include <vector>

int main()
{
    using namespace std;
    vector<int> v{1, 2, 3};

    auto a = ranges::mismatch(vector<int>{1, 2, 3}, v);
    static_assert(same_as<decltype(a.in1), ranges::dangling>);
    static_assert(same_as<decltype(a.in2), vector<int>::iterator>);

    auto b = ranges::mismatch(v, vector<int>{1, 2, 3});
    static_assert(same_as<decltype(b.in1), vector<int>::iterator>);
    static_assert(same_as<decltype(b.in2), ranges::dangling>);

    auto c = ranges::mismatch(vector<int>{1, 2, 3}, vector<int>{1, 2, 3});
    static_assert(same_as<decltype(c.in1), ranges::dangling>);
    static_assert(same_as<decltype(c.in2), ranges::dangling>);
}

Command:

g++-16 -std=c++20 -Wall -Wextra -c mismatch.cc

Expected: compilation succeeds.
Actual: all assertions expecting ranges::dangling fail.

Specified return type:
https://eel.is/c++draft/alg.mismatch

Reproduced with GCC 16.0.1 20260315 (experimental), trunk
r16-8100-g3aca3bae8ee, Ubuntu package 16-20260315-1ubuntu1~22~ppa1, targeting
x86_64-linux-gnu.

A proposed fix with regression tests is attached.

Reply via email to