【发布时间】:2020-06-22 12:31:46
【问题描述】:
为什么我不能用 GCC 主干比较指向函数的指针?
using f_t = int(*)(int);
f_t a { nullptr }, b { nullptr };
auto c = a <=> b;
int main (){}
给予
a.cpp:4:13: error: invalid operands of types ‘f_t’ {aka ‘int (*)(int)’} and ‘f_t’ {aka ‘int (*)(int)’} to binary ‘operator<=>’
4 | auto c = (a <=> b) == 0;
| ~ ^~~ ~
| | |
| | f_t {aka int (*)(int)}
| f_t {aka int (*)(int)}
【问题讨论】:
-
当
<=>不相等时,您期望返回什么?函数的地址甚至不如普通地址有用(编译之间会有所不同,如果一个函数来自加载了 ASLR 的单独 DLL/共享对象,则甚至会因运行而异);确定哪个“较小”和哪个“更大”没有任何用处。 Earlier C++ specs only defined==/!=for function pointers;我不希望他们从那以后扩展它。 -
这能回答你的问题吗? How to compare pointers?
标签: c++ c++20 spaceship-operator