我有一个结构体,其中有一些POD类型和一个字符串成员。我想使用默认的“飞船运算符”来允许对我的结构进行等式操作,但是我在字符串成员方面遇到了一些问题。据我所知,应该支持
std::string
的 <=>
,但实际上似乎并非如此。 我有一个最小化的复现示例,以下是我从clang(版本14)获得的结果警告。奇怪的是,在godbolt中,即使回到clang 12,这个示例也不会产生任何警告/错误。我真的很感激关于我理解方面的指针,因为我对这个问题感到相当困惑。#include <compare>
#include <string>
enum class MyEnum
{
ZERO = 0,
ONE = 1
};
struct MyStruct
{
float a{};
int b{};
std::string c{};
MyEnum d{};
auto operator<=>(const MyStruct&) const = default;
};
int main()
{
MyStruct my_struct;
MyStruct my_other_struct;
if(my_struct == my_other_struct)
{
return 0;
}
return 1;
}
$ clang --std=c++20 -Werror test.cpp
test.cpp:16:10: error: explicitly defaulted three-way comparison operator is implicitly deleted [-Werror,-Wdefaulted-function-deleted]
auto operator<=>(const MyStruct&) const = default;
^
test.cpp:13:17: note: defaulted 'operator<=>' is implicitly deleted because there is no viable three-way comparison function for member 'c'
std::string c{};
^
1 error generated.
$ clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
std::string
并且应该有效。 - sklottauto
改为std::strong-ordering
,它会编译吗?我不知道这个版本的 clang 是否支持该特性,也没有检查的方法。 - Barry