There are two important aspects for debugging constexpr
functions.
1) Make sure they compute the correct result
Here you can use regular unit-testing, asserts or a runtime debugger to step through your code. There is nothing new compared to testing regular functions here.
2) Make sure they can be evaluated at compile-time
This can be tested by evaluating the function as the right-hand side of a constexpr
variable assignment.
constexpr auto my_var = my_fun(my_arg);
In order for this to work, my_fun
can a) only have compile-time constant expression as actual arguments. I.e. my_arg
is a literal (builtin or user-defined) or a previously computed constexpr
variable or a template parameter, etc, and b) it can only call constexpr
functions in its implementation (so no virtuals, no lambda expressions, etc.).
Note: it is very hard to actually debug the compiler's implementation of code generation during the compile-time evaluation of your constexpr
function. You would have to attach a debugger to your compiler and actually be able to interpret the code path. Maybe some future version of Clang will let you do this, but this is not feasible with current technology.
Fortunately, because you can decouple the runtime and compile-time behavior of constexpr
functions, debugging them isn't half as hard as debugging template metaprograms (which can only be run at compile-time).