为什么全球太空飞船运营商的行为不符合预期?

问题描述 投票:0回答:1
#include <compare>
#include <forward_list>

template<typename T>
struct A
{
    std::forward_list<T> l;
};

template<typename T>
auto operator<=>(const A<T>& lhs, const A<T>& rhs)
{
    return lhs.l <=> rhs.l;
}

int main()
{
    std::forward_list<int>{} < std::forward_list<int>{}; // ok

    A<int>{} < A<int>{}; // error
}

编译有clang++ -std=c++20 -stdlib=libc++ main.cpp和错误消息:

main.cpp:13:18: error: invalid operands to binary expression ('const std::forward_list<int>' and 'const std::forward_list<int>')
    return lhs.l <=> rhs.l;
           ~~~~~ ^   ~~~~~
main.cpp:20:14: note: in instantiation of function template specialization 'operator<=><int>' requested here
    A<int>{} < A<int>{}; // error

为什么全球太空飞船运营商的行为不符合预期?

c++ clang standards c++20 spaceship-operator
1个回答
3
投票

似乎libc ++(或任何标准库)尚未完全实现飞船操作员库的附加功能。

请参见cppreference.com上的here for libc++here了解已编译表。将operator<=>添加到std::forward_list的相关文章为P1614。

© www.soinside.com 2019 - 2024. All rights reserved.