c++clangllvmlibc++

What libc++ version supports three way comparison for STL types


I'm trying to compile simple code with C++20 support, clang 14/15 and libc++ 14/15 The code

#include <iostream>
#include <string>

struct foo {
    std::string member{"Hello, Kitty!"};
    auto operator<=>(const foo&) const = default;
};
int main() {
    foo f;
    std::cout << f.member << std::endl;
    return 0;
}

The CMakeList

cmake_minimum_required(VERSION 3.25)
project(three_way_comparison)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
add_compile_options("-stdlib=libc++")

add_executable(three_way_comparison main.cpp)

It fails with

> /home/user/CLionProjects/three-way-comparison/main.cpp:6:10: warning:
> explicitly defaulted three-way comparison operator is implicitly
> deleted [-Wdefaulted-function-deleted]
>     auto operator<=>(const foo&) const = default;
>          ^ /home/user/CLionProjects/three-way-comparison/main.cpp:5:17: note:
> defaulted 'operator<=>' is implicitly deleted because there is no
> viable three-way comparison function for member 'member'
>     std::string member{"Hello, Kitty!"};
>                 ^ /usr/lib/llvm-14/bin/../include/c++/v1/__utility/pair.h:340:1: note:
> candidate template ignored: could not match 'pair' against
> 'basic_string' yada, yada

the /usr/lib/llvm-14/include/c++/v1/string header indeed does not have the operator<=> in it. tried to run sudo apt to install libc++-14-dev, it didnt work. What I'm doing wrong? version 15 ends up with the same error.


Solution

  • What libc++ version support three way comparison for STL types

    The one that comes with clang++ 16.

    Demo