<=> Operator behaves differently for x86 and arm

Viewed 100

I have the following code which compiles correctly on godbolt:

#include <array>
#include <iostream>

template <class T>
class A
{
    public:
    std::array<T,2> data_;
    constexpr friend auto operator<=>(const A<T>& a, const A<T>& b)
    {
        return  ( a.data_ <=> b.data_ );
    }
};

int main()
{
    using B = A<int>;

    auto a = B{0,0};
    auto b = B{1,1};

    std::cout << std::boolalpha;
    std::cout << (a.data_ <b.data_ );
    std::cout << (a<b);

}

The same program on Arm Mac, compiling with

clang++ spacetest.cpp -o spaceship -std=c++20

gives me the following error

spacetest.cpp:11:27: error: invalid operands to binary expression ('const std::array<int, 2>' and 'const std::array<int, 2>')
        return  ( a.data_ <=> b.data_ );

The compiler version is

clang++ -v
Homebrew clang version 13.0.0
Target: arm64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

So, I dunno what I'm doing wrong here. Appreciate hints.

1 Answers

I think @cpplearner his right with his comment. The <=>operator is supported in libstdc++ since version 10.1 (see table 1.9 here https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2020). The compilerexplorer uses the v8.2 toolchain for arm, so it's not yet supported. For x64, it uses v11.2, so it is supported.

You can check for the version by adding the -v compiler flag on the command line.

Related