Question

In C++14 we get upgraded version of constexpr meaning that now it will be possible to use loops, if-statements and switches. Recursion is already possible as in C++11.

I understand that constexpr functions/code should be quite simple, but still the question arise: how to effectively debug it?

Even in "The C++ Programming Language, 4th Edition" there is a sentence that debugging can be hard.

Was it helpful?

Solution

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).

OTHER TIPS

The answer I wrote on 3 April '15 is clearly wrong. I can't understand what I was thinking.

Here is the "real" answer - the method I use now.

a) write your constexpr function as you normally would. So far it doesn't work.

b) when the function is invoked at compile time - compilation fails with nothing more than a message to the effect "invalid constexpr" function. This makes it hard to know what the problem actually is.

c) Make small test program which calls the function with parameters known only at runtime. Run your test program with the debugger. You'll find that you can trace through the function in the normal manner.

It took me an embarrassingly long time to figure this out.

If you are using gcc, you can try this

and there is a introduce about it

If by debugging you mean "make it known that a certain expression is not of a desired value", you could check it at runtime

#include <stdexcept>
#include <iostream>

constexpr int test(int x){ return x> 0 ? x : (throw std::domain_error("wtf")); }

int main()
{
  test(42);
  std::cout<< "42\n";
  test(-1);
  std::cout<< "-1\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top