Live data from Hacker News

C++17 constexpr everything, or as much as the compiler can

solarianprogrammer.com

11–20 of 120 posts

Re: C++17 constexpr everything, or as much as the compiler can

#12
post #10

Genuine question: Is there anything protecting us from an entirely new class of compiler bugs where the constexpr code and the compiled code behave differently? Is it at all possible?

> Is it at all possible?

Of course it is possible. Just consider all the registers, memory locations changed during the execution of such functions. Such side effects can affect behaviour of other code in the presence of compiler bugs.

Re: C++17 constexpr everything, or as much as the compiler can

#13
All of this is easy in Common Lisp, which includes interactive compile-time debugging. You can use #., EVAL-WHEN, LOAD-TIME-VALUE, and other things to control evaluation time, and use the same, usual, interactive debugger that you use for runtime code, without any extra infrastructure or scaffolding.

In C++, to debug failed constexpr’s, often, you have to make it a non-constexpr and debug at runtime.

Re: C++17 constexpr everything, or as much as the compiler can

#14
post #3
post #2

What about debugging? If the code to generate the data is large and complex, how do you debug it when it only runs at compile time? Is the solution to start with two programs like the author and then merge them? That still seems like a maintainability issue.

You could use something like #ifdef NDEBUG #define MY_CONSTEXPR #else #define MY_CONSTEXPR constexpr so when you are running the debug build, everything is debuggable.

That makes the compiler not complain when your constexpr isn't a valid constexpr.

Re: C++17 constexpr everything, or as much as the compiler can

#15
I'm not sure this is really necessary. gcc and Apple's clang optimize the factorial code without "constexpr" and the fibonacci code might be an extreme case avoided by reasonable defaults for the numerous optimization parameters that can be adjusted:

  max-inline-recursive-depth
  max-inline-recursive-depth-auto
  Specifies the maximum recursion depth used for recursive   inlining.
  For functions declared inline, --param max-inline-recursive-depth is taken into
  account. For functions not declared inline, recursive inlining happens only when
   -finline-functions (included in -O3) is enabled and --param
  max-inline-recursive-depth-auto is used. The default value is 8.

Re: C++17 constexpr everything, or as much as the compiler can

#16

ad hoc partial evaluation ?

With explicit purity marks.

I don't see how the C++ community will benefit from those, since the type of code that benefits is normally done in other languages. But I am certainly less creative than a community.

Re: C++17 constexpr everything, or as much as the compiler can

#17

C++ is never just C++. The build system for any C++ program includes both macro language and some form of make and/or make replacement. Explicitly generating tables at compile time and linking is bog standard at this point. Write a table generator program, have output create a table, splice in the order into make and/or make replacement. Sure it's three extra steps, but it's small steps that can be debugged. It also…

And have all embedded engineers raging because you just made cross compilation an order of magnitude harder.

Re: C++17 constexpr everything, or as much as the compiler can

#18
post #7
post #2

What about debugging? If the code to generate the data is large and complex, how do you debug it when it only runs at compile time? Is the solution to start with two programs like the author and then merge them? That still seems like a maintainability issue.

constexpr don't have to run at compile-time, they can do so. So you can use regular tests/debugging to debug your function.

what about cases when regular debugging is printf/assert/static_assert?

Re: C++17 constexpr everything, or as much as the compiler can

#19
>In conclusion, at the time of this writing, you need to inspect the generated code of your compiler, if you want to be sure that something is really calculated at compile time.

I don't get this. Why? Doesn't making a variable constexpr ensure it is a compile time value, or it will fail to compile?

Re: C++17 constexpr everything, or as much as the compiler can

#20

C++ is never just C++. The build system for any C++ program includes both macro language and some form of make and/or make replacement. Explicitly generating tables at compile time and linking is bog standard at this point. Write a table generator program, have output create a table, splice in the order into make and/or make replacement. Sure it's three extra steps, but it's small steps that can be debugged. It also…

Why do you say writing auxiliary programs and tools to integrate the data is "standard practice", and a simple and straightforward language feature are "advanced compiler tricks"?
Post reply on HN