问题
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