Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

constexpr

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?

like image 670
odinthenerd Avatar asked Jul 10 '13 18:07

odinthenerd


1 Answers

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.

like image 53
Andy Prowl Avatar answered Sep 21 '22 19:09

Andy Prowl