【发布时间】:2020-04-08 08:08:09
【问题描述】:
#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
为什么全球飞船操作员的行为不符合预期?
【问题讨论】:
-
你能让问题文本更具体到实际问题吗?什么是“不期望”?在这种情况下,问题似乎是“为什么我不能在
std::forward_list上调用<=>?”全局宇宙飞船运算符的行为似乎完全符合您的要求 - 毕竟它被调用了。
标签: c++ clang standards c++20 spaceship-operator