【问题标题】:static_assert on inline function gives error内联函数上的 static_assert 给出错误
【发布时间】:2015-11-09 18:33:55
【问题描述】:

考虑以下情况

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 不应引发非常量条件错误。

我错了吗?

【问题讨论】:

  • 除非在某处声明了foo,否则我希望这根本不会编译...
  • 在 clang++ 3.7 [大约在 3.7-RC1 之前的一两周] 中工作,而不是在 gcc 4.9.2 中。怀疑是gcc的bug。
  • 这个GCC bugzilla thread和这个问题有关吗? Clang3.6可以编译bugzilla中的代码,但是gcc5+和gcc6HEAD不行。

标签: c++ function templates inline static-assert


【解决方案1】:

当一个函数被内联时,指向该函数的指针不存在。所以我们不能和nullptr比较。

函数最终是否被内联,取决于编译器。 inline 关键字不能保证这一点。

【讨论】:

  • 错误:如果你获取一个函数的地址(即使是inlined),编译器应该给它(而不是总是内联那个函数)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
相关资源
最近更新 更多