Mac 中编译通过的代码在 Linux 中编译不通,问题在于 enum 中使用了 htonl:
enum A {
X = htonl(1),
};
报错:
./****: error: expression is not an integral constant expression
X = htonl(1),
^~~~~~~~
/usr/include/netinet/in.h:404:21: note: expanded from macro 'htonl'
# define htonl(x) __bswap_32 (x)
^~~~~~~~~~~~~~
./****: note: non-constexpr function '__bswap_32' cannot be used in a constant expression
而 Mac 中 htonl 是这样的:
#define htonl(x) __DARWIN_OSSwapInt32(x)
#define __DARWIN_OSSwapInt32(x) \
(__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt32(x) : _OSSwapInt32(x))
如果是常量,直接用 __DARWIN_OSSwapConstInt32 宏算出结果,不是常量则在运行时 bswap 计算。所以 htonl(常量) 能在 enum 中使用。
为什么 Linux 没有实现这个特性?
Linux: 5.4.0-126-generic (clang version 10.0.0-4ubuntu1)
MacOS: 22.1.0 Darwin (Apple clang version 14.0.0)