Hacker News new | past | comments | ask | show | jobs | submit login

Wow.

How does one debug complex constexpr code? I assume there's no printf.

It'd be really cool if it supported some kind of interpolation, like Jinja templates, so it could generate dynamic page templates at compile time.




Often times, the easiest way to debug complex template code is to intentionally fail the compilation, with a minimal context, so you can read the compiler's output on what the types and values are. Here's a simple example of a function I used to use:

    template <typename T, typename ...Ts>
    void show_types()
    { static_assert((T*)nullptr, "Type log"); }

    int main()
    {
      show_types<int, float, bool>();
    }
When trying to compile this, using something like `g++ show-type.cpp`, you'll get this output:

    show-type.cpp: In instantiation of ‘void show_types() [with T = int; Ts = {float, bool}]’:
    show-type.cpp:7:32:   required from here
    show-type.cpp:3:3: error: static assertion failed: Type log
     { static_assert((T*)nullptr, "Type log"); }
The same can be done for non-type template args, of course.


I prefer this:

T::asdfasdf();

Although I might start using yours.


constexpr is a subset of what you can do at runtime. So my way of debugging is

#define constexpr

Which makes all the code plain code that executes at runtime, then you can put your couts and use your debuggers.

Its orders of magnitude simpler than dealing with template meta-programming.

constexpr_printf is the feature I want most of all. There is actually a patch for gcc that implements this.

I dont know how Jinja works, but the idea here is you do templating at runtime, but your templates get compiled down to a tree like DS at compiletime


Jinja compiles to Python which is then compiled to CPython bytecode and executed by the CPython VM.


I see - here the compile time data structure is converted into a tree at runtime and rendered.

I decided to use a runtime DS to allow things like splicing and looping templates etc.

It's also possible to make the runtime render the compiled time DS directly, which should be even faster if your requirements are simple enough


In Dlang, where compile time function execution is the norm, you use the equivalent of what would be "pragma printf" -- eg. You embed compiler warnings into your compile time branching to debug.


One can also just static assert with a ctfe-able message.


https://github.com/saarraz/static-print (not mine) is a compiler patch that adds the kind of print you're looking for




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: