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

            Bug ID: 109738
           Summary: C++20 implicit conversion is used during spaceship
                    operator resolution instead of class's operator< for
                    classes without spaceship operator
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: szhong at perforce dot com
  Target Milestone: ---

Using -std=c++20 -- implicit conversion is used during spaceship operator
resolution instead of class's operator< for classes without spaceship operator


testcase:
#include <string>
#include <list>
#include <map>
#include <iostream>

class StringWrapper {
public:
    StringWrapper(const char* s)
        : str_(s) {}
    std::string str_;
    operator const char* () const;
};

inline
StringWrapper::operator const char* () const
{
    return str_.data();
}

std::ostream&
operator<<(std::ostream& os, const StringWrapper& str)
{
    os << str.str_;
    return os;
}

bool
operator< (const StringWrapper& lhs, const StringWrapper& rhs)
{
    return lhs.str_ < rhs.str_;
}

#if defined(SPACE_SHIP)
auto operator<=>(const StringWrapper& lhs, const StringWrapper& rhs)
{
    return lhs.str_ <=> rhs.str_;
}
#endif

void print_map(std::string_view comment, const
std::map<std::list<StringWrapper>, std::list<StringWrapper> >& m)
{
    std::cout << comment;
    // iterate using C++17 facilities
    for (const auto& [key, value] : m) {
        std::cout << '[';
        for (const auto& i : key)
            std::cout << i << ", ";
        std::cout << "];";
    }
    std::cout << '\n';
}


int main()
{
    StringWrapper a("Diane");
    StringWrapper b("Harry");
    StringWrapper c("Sally");
    StringWrapper d("George");

    std::list<StringWrapper> l1;
    l1.push_back(a);
    l1.push_back(c);

    std::list<StringWrapper> l2;
    l2.push_back(b);
    l2.push_back(d);

    std::map<std::list<StringWrapper>, std::list<StringWrapper> > m1;

    m1.insert(m1.end(), std::map<std::list<StringWrapper>,
std::list<StringWrapper> >::value_type(l1, l2));
    m1.insert(m1.end(), std::map<std::list<StringWrapper>,
std::list<StringWrapper> >::value_type(l2, l1));

    print_map("m1: ", m1);

    std::map<std::list<StringWrapper>, std::list<StringWrapper> > m2;

    m2.insert(m2.end(), std::map<std::list<StringWrapper>,
std::list<StringWrapper> >::value_type(l2, l1));
    m2.insert(m2.end(), std::map<std::list<StringWrapper>,
std::list<StringWrapper> >::value_type(l1, l2));

    print_map("m2: ", m2);
}

$ g++ -std=c++20 -Wall -Wextra 20_list_compare_wrapper.cpp; ./a.out;
m1: [Harry, George, ];[Diane, Sally, ];
m2: [Diane, Sally, ];[Harry, George, ];

The problem is the std:map is no longer sorted according to StringWrapper but
sorted according const char* instead.

The debugger showed during resolution of spaceship operator, the implicit
conversion to const char* for StringWrapper is selected instead of
StringWrapper::operator<() during insertion into std::map with the key of
std::list<StringWrapper>.

This problem occurs with gcc 12.1.1 as well.


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-11
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet
--with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

system type: $ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/";
SUPPORT_URL="https://help.ubuntu.com/";
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/";
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy";
UBUNTU_CODENAME=jammy

Reply via email to