GCC 4.9 在检查内联函数的指针时出错(带 static_assert)

GCC 4.9 gives error when checking inline function's pointer (with static_assert)

本文关键字:出错 static assert 指针 检查 函数 GCC      更新时间:2023-10-16

考虑以下情况

typedef void (*foo)();
template<foo f>
struct bar {
     static_assert(f!=nullptr,"f == null!");
};
void baz() {}
inline void bax() {  }
bar<baz> ok;
bar<bax> bad; // error: non-constant condition for static assertion

bazbax都可以作为模板参数。它表明两者都被接受为常量。然而,在static_assert它们似乎是不同的(至少在gcc 4.9) - bax不再是一个常数。

我的假设是static_assert和模板评估常数相同。例如,两个错误都应该是

  • 'bax不是一个有效的模板参数'或
  • static_assert不应该引发非恒定条件错误。

我错了吗?

又一个GCC错误,更新到新版本,或者迁移到LLVM (clang)。

详情请参阅问题单:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036

内联函数时,指向该函数的指针不存在。因此不能与nullptr进行比较。

函数最终是否内联取决于编译器。inline关键字不保证。