Quick Q: constexpr specifier performance did't meet my expectations in C++

Quick A: a constexpr function does not imply it will be evaluated at compile time.

Recently on SO:

constexpr specifier performance did't meet my expectations in C++

Your constexpr function requires way too much computation to do in a compiler, that's why the compiler chooses to delay it to runtime execution.

You can change this line:

static const auto c_x = c_fun(40);

to:

constexpr auto c_x = c_fun(40);

to see compiler's output. On clang, it tells me:

note: constexpr evaluation hit maximum step limit;
c_x is indeed a compile-time constant, but the compiler can't compute it due to implementation limitation. Note that your function has exponential complexity.

Everything will be fine if you change 40 to some reasonable number, like 10:

constexpr auto c_x = c_fun(10);

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.