Why is comparing two parameters of a constexpr function not a constant condition for static assertion?

我的梦境 提交于 2019-11-26 23:22:54

问题


constexpr uint32_t BitPositionToMask(int i,int Size){
static_assert(i < Size,"bit position out of range");
return 1 << i;
}

this generates:

error: non-constant condition for static assertion

on GCC 4.6.2 Am I not getting something or is this a GCC bug?

Update: thank you Andy for being my nerd guardian angel again. Since I have the values at compile time anway I just made it a template and it works as intended.

template<int i,int Size>
constexpr uint32_t BitPositionToMask(){
    static_assert(i < Size,"bit position out of range");
    return 1 << i;
}

回答1:


A constexpr function can also be invoked with arguments evaluated at run-time (in that case, it just gets executed just like any regular function). See, for instance, this live example.

A static_assert(), on the other hand, strictly requires its condition to be a constant expression that can be evaluated at compile time.



来源:https://stackoverflow.com/questions/17577749/why-is-comparing-two-parameters-of-a-constexpr-function-not-a-constant-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!