[Bug libstdc++/104606] New: Regression in comparison operator resolution with std::optional

julien.philippon at epitech dot eu gcc-bugzilla@gcc.gnu.org
Sat Feb 19 16:03:27 GMT 2022


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

            Bug ID: 104606
           Summary: Regression in comparison operator resolution with
                    std::optional
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: julien.philippon at epitech dot eu
  Target Milestone: ---

Created attachment 52480
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52480&action=edit
Preprocessed file trigering the regression

This code used to compile with G++ 11.1 but does not anymore with G++ 11.2 :

```
#include <optional>
#include <variant>
#include <vector>

struct Value : public std::variant<int, std::vector<Value>> {
    using variant::variant;
};

struct Comparator {
    template <typename T>
    bool operator<=(const T &rhs)
    {
        return true;
    }
};

int main()
{
    auto test = Comparator() <= Value{1};
    auto test2 = Comparator() <= std::make_optional(Value{1});
}
```

In GCC 11.1 the comparison correctly deduced in the two cases that the lower or
equal operator of Comparator should be used, but in GCC 11.2, the second
comparison which involve an optional fails to compile.

It looks like GCC is trying to instantiate the three-way comparison operator of
Value, instead of using the operator of Comparator.

Output from g++ -v :

```
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-linker-build-id --enable-lto
--enable-multilib --enable-pgo-build=lto --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror --with-build-config=bootstrap-lto
--enable-link-serialization=1 gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (GCC)
```


More information about the Gcc-bugs mailing list