#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
为什么全球太空飞船运营商的行为不符合预期?
似乎libc ++(或任何标准库)尚未完全实现飞船操作员库的附加功能。
请参见cppreference.com上的here for libc++和here了解已编译表。将operator<=>
添加到std::forward_list
的相关文章为P1614。