
constexpr vs. static constexpr
constexpr and static constexpr variables could produce very different code.
• Stack-local constexpr variable can be slightly faster than static constexpr for
sizes less than 136–144 bytes .
• Larger data, such as greater than ∼2KB, copying into the stack becomes very expensive,
making static constexpr much faster.
• static constexpr is faster with lower optimization options ( -O0 , -O1 ).
• clang and msvc are generally emits identical code in both cases.
• constexpr variable with dynamic indexing produces very inefficient code with GCC.
//constexpr int array[] = {1,2,3,4,5,6,7,8,9}; // bad performance with GCC
static constexpr int array[] = {1,2,3,4,5,6,7,8,9};
return array[index];
C++ Weekly - Ep 315 - constexpr vs static constexpr
18/93