考虑以下情况
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
baz
和 bax
都被接受为模板参数。
它表明两者都被接受为常数。
然而,在 static_assert
他们似乎是不同的(至少在 gcc 4.9 中) - bax
不再是一个常数。
我的假设是 static_assert
和模板评估常数相同。
例如。任何一个错误都应该是
- 'bax 不是有效的模板参数'或
static_assert
不应引发非常量条件错误。
我错了吗?
最佳答案
当一个函数被内联时,指向该函数的指针不存在。所以我们不能和nullptr比较。
函数最终是否被内联,取决于编译器。 inline
关键字不保证这一点。
关于c++ - 内联函数上的 static_assert 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32045025/