[英]Recursive spaceship operator
我写了这个简单的代码,但它没有编译,因为比较被隐式删除了。
struct Tree {
std::vector<Tree> child;
friend auto operator<=>(const Tree &a, const Tree &b) = default;
}
int main(){
Tree t;
std::cout<<(t<t)<<std::endl;
}
任何人都可以向我解释如何解决这个问题,或者至少为什么它不起作用?
编辑:使用“g++ -std=gnu++2a main.cpp”编译 Edit2:这是 output 的错误部分(后面有很多很多行候选):
main.cpp: In function 'int main()':
main.cpp:31:25: error: use of deleted function 'constexpr auto operator<=>(const Tree&, const Tree&)'
31 | std::cout << (tmp < tmp) << std::endl;
| ^~~
main.cpp:12:17: note: 'constexpr auto operator<=>(const Tree&, const Tree&)' is implicitly deleted because the default definition would be ill-formed:
12 | friend auto operator<=>(const Tree &a, const Tree &b) = default;
| ^~~~~~~~
main.cpp:12:17: error: no match for 'operator<=>' (operand types are 'std::vector<Tree>' and 'std::vector<Tree>')
您通常不能定义具有推导返回类型的递归 function。 为了推断它,你需要已经知道它,所以这是一个没有 go。
用显式返回类型std::weak_ordering
替换auto
可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.