简体   繁体   English

为什么全球飞船运营商的行为不如预期?

[英]Why does global spaceship operator not behave as expected?

#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
}

Compiled with clang++ -std=c++20 -stdlib=libc++ main.cpp and error messages: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

Why does global spaceship operator not behave as expected?为什么全球飞船运营商的行为不如预期?

It does not seem that libc++ (or any of the standard libraries) implements the spaceship operator library additions completely yet. libc++(或任何标准库)似乎还没有完全实现宇宙飞船运算符库的添加。

See here for libc++ and here for a compiled table at cppreference.com.有关libc++ 的信息,请参见此处,cppreference.com 上的编译表请参见此处。 The relevant paper adding the operator<=> to std::forward_list is P1614.operator<=>添加到std::forward_list的相关论文是 P1614。

If you look at libc++'s source code for std::forward_list here , you see that there is no mention of operator<=> yet and the other secondary comparison operators are still defined unconditionally (which should not be the case in C++20).如果您在此处查看 libc++ 的std::forward_list源代码,您会看到尚未提及operator<=>并且其他辅助比较运算符仍然是无条件定义的(在 C++20 中不应该是这种情况) .

std::forward_list<int>{} < std::forward_list<int>{}; compiles because it uses operator< , not operator<=> .编译,因为它使用operator< ,而不是operator<=> If you tried std::forward_list<int>{} <=> std::forward_list<int>{} directly, it would fail as well (in the current state of libc++).如果您直接尝试std::forward_list<int>{} <=> std::forward_list<int>{} ,它也会失败(在 libc++ 的当前 state 中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么const_cast的行为不符合预期? - Why does const_cast not behave as expected? 为什么这个模板函数没有按预期运行? - Why does this template function not behave as expected? 为什么enable_if的行为不符合预期? - Why does enable_if not behave as expected? 当父 class 没有为自己定义宇宙飞船运算符时覆盖宇宙飞船运算符 - Overriding spaceship operator when parent class does not define spaceship operator for itself 为什么[std :: is_move_assignable]的行为不符合预期? - Why does [std::is_move_assignable] not behave as expected? 为什么std :: is_copy_constructible的行为不符合预期? - Why does std::is_copy_constructible not behave as expected? 为什么 std::future::wait_for 的行为不符合预期? - Why does std::future::wait_for not behave as expected? 为什么std :: chrono :: time_point的行为不符合预期? - Why does std::chrono::time_point not behave as expected? 为什么std :: add_lvalue_reference的行为不符合预期? - Why does std::add_lvalue_reference not behave as expected? 为什么C ++的`变量模板&#39;没有按预期运行? - Why does C++'s `variable template` not behave as expected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM