#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
为什么全球飞船运营商的行为不符合预期?