Search code examples
c++clangc++20clang++

namespace "std" has no member "lexicographical_compare_three_way" on macOS clang


I have been trying to compile this example from cppreference

#include <algorithm>
#include <cctype>
#include <compare>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <utility>
using namespace std::literals;
 
void show_result(std::string_view s1, std::string_view s2, std::strong_ordering o)
{
    std::cout << std::quoted(s1) << " is ";
    std::is_lt(o) ? std::cout << "less than ":
    std::is_gt(o) ? std::cout << "greater than ":
                    std::cout << "equal to ";
    std::cout << std::quoted(s2) << '\n';
}
 
std::strong_ordering cmp_icase(unsigned char x, unsigned char y)
{
    return std::toupper(x) <=> std::toupper(y);
};
 
int main()
{
    for (const auto& [s1, s2] :
    {
        std::pair{"one"sv, "ONE"sv}, {"two"sv, "four"sv}, {"three"sv, "two"sv}
    })
    {
        const auto res = std::lexicographical_compare_three_way(
            s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(), cmp_icase);
        show_result(s1, s2, res);
    }
}

I works fine on godbolt but it doesn't seem to compile on my macOS M1

I have been using these commands to compile:

g++ -std=c++20 test.cpp # Apple Clang 15.0.0
clang++ -std=c++20 test.cpp # Homebrew clang version 16.0.6

this is the error:

test.cpp:31:31: error: no member named 'lexicographical_compare_three_way' in namespace 'std'; did you mean 'lexicographical_compare'?
        const auto res = std::lexicographical_compare_three_way(
                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                              lexicographical_compare
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/lexicographical_compare.h:42:1: note: 'lexicographical_compare' declared here
lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
^

Solution

  • I posted this on llvm-project's github page and they said this is only available from libc++ 17

    Implementation commit