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

            Bug ID: 117552
           Summary: ranges::(stable_)sort fail to accept C++20-only random
                    access ranges
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: de34 at live dot cn
  Target Milestone: ---

The following example doesn't compile with libstdc++.

Godbolt link: https://godbolt.org/z/67Tanfxad


```
#include <cstdint>
#include <algorithm>
#include <ranges>

constinit int a[]{42, 1729};
constinit auto view32 =
    std::views::iota(std::uint32_t{}, std::uint32_t{}) |
    std::views::transform([] (std::uint32_t i) -> int& { return a[i]; });
constinit auto viewmax =
    std::views::iota(std::uintmax_t{}, std::uintmax_t{}) |
    std::views::transform([] (std::uintmax_t i) -> int& { return a[i]; });

int main()
{
    std::ranges::sort(view32);
    std::ranges::sort(viewmax);
    std::ranges::stable_sort(view32);
    std::ranges::stable_sort(viewmax);
}

```

There're at least 3 issues concentratedly appearing in sorting algorithms.

1. make_unsigned_t is mistakenly used for difference type that is an
integer-class type.
2. iterator_traits<It>::value_type is calculated to be void for some C++20-only
iterators. Such calculation might be still right, but iter_value_t should be
used in these cases.
3. These algorithms use tag dispatch based on
iterator_traits<It>::iterator_category, which is almost always wrong when the
view is produced via views::iota.

Presumably, there might be a common cause - these ranges algorithms are using
non-ranges versions too eagerly.

Reply via email to