簡體   English   中英

<=> 運算符對於 x86 和 arm 的行為不同

[英]<=> Operator behaves differently for x86 and arm

我有以下代碼可以在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);

}

Arm Mac上的相同程序,編譯

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

給我以下錯誤

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_ );

編譯器版本是

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

所以,我不知道我在這里做錯了什么。 欣賞提示。

我認為@cpplearner 他的評論是正確的。 從版本 10.1 開始,libstdc++ 支持<=>運算符(請參見此處的表 1.9 https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2020 )。 compilerexplorer 使用 arm 的 v8.2 工具鏈,因此尚不支持。 對於 x64,它使用 v11.2,因此受支持。

您可以通過在命令行上添加-v編譯器標志來檢查版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM