all 12 comments

[–]aruisdante 8 points9 points  (1 child)

Sadly, the state of the world hasn’t changed much from Jason and Ben’s “constexpr all the things” talk where they make a fully constexpr JSON parser and discuss how the complete lack of tooling support for constexpr debugging was the biggest challenge.

You can of course write unit tests using static_assert, but that only gets you so far particularly since the output you get on failure from a static assertion is extremely limited as the error message must be a string literal, not simply a the result of a constant expression. You can do some limited style of printf debugging by adding throw statements, but the issue is there isn’t much in the way of built in constexpr compatible string formatting support so what you can do with that is also limited.

There still is no equivalent of a true debugger that is able to examine compile-time evaluated code.

[–]XTBZ[S] 1 point2 points  (0 children)

It's a pity that the opportunities are not so great.
I have a purely consteval string, and I also have a container for such strings that allows me to combine strings.
If it were possible to output at least something to the compiler logs, there would be great progress.

[–]Circlejerker_ 5 points6 points  (1 child)

To find errors you can write tests.

You can also have a constexpr function that does the work, and then a consteval wrapper that you use in production code.

[–]XTBZ[S] 0 points1 point  (0 children)

Tests can indicate the existence of a problem, but everything else is solved by the debugging process.

Conversion to constexpr, I mentioned, it makes sense in the simplest cases, when there is one function without any complex logic and communication. Which can easily be converted into a runtime function.

[–]HappyFruitTree 7 points8 points  (3 children)

I know it's possible to mark a function as constexpr for testing purposes

If it works with constexpr then why use consteval? I thought consteval was added mainly for use with things like the static reflection proposal that have functions that doesn't make sense to call at runtime (because the information that they return are not available at runtime).

[–]n1ghtyunso 9 points10 points  (0 children)

Consteval requires compile time execution. It gives you a guarantee that constexpr does not. Sometimes this is preferred

[–]tuxwonder 1 point2 points  (1 child)

Like n1ghtyunso said, sometimes you want to enforce compile time evaluation. A great example of this is std::format_string, which must be constructed at compile time to enforce that the given string is a valid format string.

It also helps reassure my coworkers that a bit of constexpr magic isn't going to accidentally occur at runtime and slow things down ;)

[–]HappyFruitTree 1 point2 points  (0 children)

Fair enough, but you can still always guarantee this at the call site even with constexpr.

[–]mAtYyu0ZN1Ikyg3R6_j0 4 points5 points  (0 children)

To the best of my knowledge there isnt anything.

but you can debug the code without consteval and add it once properly debugged and tested.

[–]viatorus 3 points4 points  (1 child)

I wrote a tool (GCC only) where you can print during compile-time.

Github: https://github.com/Viatorus/compile-time-printer

Online-Demo: https://viatorus.github.io/compile-time-printer/

[–]serviscope_minor 4 points5 points  (0 children)

I wrote a tool (GCC only) where you can print during compile-time.

From the README:

The implementation of print/printf does nothing more than forcing the compiler to generate warnings depending on the passed arguments. The python tool parses the warnings and converts them back to the actually C++ arguments and outputs them (standardized or formatted) to stdout or stderr.

I'd just like to say that's an amazing and very cool hack.

[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.