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

            Bug ID: 127141
           Summary: Incorrect behavior from std::find/std::ranges::find
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: irvsrc at gmail dot com
  Target Milestone: ---

In some contexts (as shown in the example below), `std::find` and
`std::ranges::find` both produce incorrect results depending on constant
evaluation and other surrounding logic that should not affect the result.

Example:

```
auto constexpr inline data = std::string_view{"__a_"};

auto constexpr fn(auto first, auto last) -> bool { // templated so the example
is portable
    // {'_', '_', 'a', '_'}
    ++first; // {'_', 'a', '_'} <---- removing this = no error
    first = std::ranges::find(first, last, 'a'); // {'a', '_'}
    ++first; // {'_'} <---- removing this = no error
    return first != last; // should always be true
}

auto main() -> int {
    // 1)
    auto constexpr r1 = fn(std::ranges::begin(data), std::ranges::end(data));
    static_assert(r1); // ok

    // 2)
    auto r2 = fn(std::ranges::begin(data), std::ranges::end(data));
    assert(r2); // error

    // ____

    // 3)
    auto data_copy = data;
    auto r3 = fn(std::ranges::begin(data_copy), std::ranges::end(data_copy));
    assert(r3); // ok
}
```

The incorrect behavior only occurs with `-std=c++26`.

Let `E` be the call to `fn`
>From the example, we can see that the incorrect behavior occurs when the
evaluation of the expression `E` satisfies the requirements for a
`constant-expression` but is not `manifestly constant-evaluated`.

gcc version 16.1.1 20260728 (GCC) 
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --with-gcc-major-version-only
--with-linker-hash-style=gnu --with-system-zlib --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-fixincludes --disable-libssp
--disable-libstdcxx-pch --disable-werror
/usr/bin/g++ -std=c++26 -Wall -Wextra -g test.cpp -o build

Reply via email to