News for hackers. Stuff that matters.
Re:C++17 constexpr Compile-time Ray Tracer (2017) by hhmc on Tuesday October 30, 2018 @08:41AM (#18335936)
There's seemingly a lot of negative responses to articles concerning constexpr in modern C++ (you can see some here in this thread). I suspect this is partially because there's a general outsized dislike of C++, and partially because typically these articles focus on toy problems where the capacity to run at compile time isn't actually beneficial, rather than cases where constexpr can provide some real benefit towards production code.

Like any syntactic construct in a turing complete language constexpr presents a convenience - in this case the ability to rectify compile time generated data and runtime code paths/data. There are some practical cases I've seen where it can be a real improvement to hygiene (i.e. you can replace a compile time script/program generating data with one unified code base). e.g.

1. Perfect hash generation. Rather than using a separate gperf run you can generate your hashes during compilation of the relevant TU.

2. Format string lookup tables. For fast formatting you can push an index and positional arguments to a queue, and handle formatted string construction in another thread. Broadly speaking lookup tables in general can be generated at compile time rather than static data imported into code.

3. Complex starting conditions for physics simulations. If you have, say, some initial matrix that doesn't change between simulation runs - you can exercise existing code to generate this matrix at compile time rather than needing a separate process run or script.

These approaches are not without their downsides - particularly if the translation units in question need to be recompiled often, or if you're using whole program optimisation - but the benefit of keeping generated data in lockstep with the code consuming it can be worthwhile.

(That being said, I personally find articles like this incredibly edifying.)

Re:C++17 constexpr Compile-time Ray Tracer (2017) by Arnt on Tuesday October 30, 2018 @09:17AM (#18336153)
4. Things you want to not run at runtime. If you can demand of the compiler that foo is run only at compile time, you can eliminate some performance outliers, with good protection against accidental later regressions. (I suppose this could apply to security too — anything run at compile time is definitely not vulnerable to crafty user input from a malicious user.)

5. My compiler is a rackmount somewhere; even if constexpr evaluation is somewhat slower than runtime code in a particular case, I may prefer to use the rackmount's plentiful power rather than the slim, light battery on the device where the final code will run.

Re:C++17 constexpr Compile-time Ray Tracer (2017) by tcbrindle on Tuesday October 30, 2018 @09:22AM (#18336187)
6. A constexpr function is guaranteed to have no undefined behaviour (when evaluated at compile time, anyway)
Re:C++17 constexpr Compile-time Ray Tracer (2017) by geokon on Tuesday October 30, 2018 @09:50AM (#18336407)
I think the most important benefit is simpler and just the code clarity for your constants. For instance if you have compile time constants for width and height you can sanely write diagonal=Pythagoras(width,height) and be done and know you have that value at compile time.

Before you'd either have to hope the compiler did that for you magically, do something ugly with the preprocessor, or bake in numbers

In a way all your examples are just more extreme version of the same but I guess my point is that it's not something fringe for fancy optimizations, it's daily use.

Re:C++17 constexpr Compile-time Ray Tracer (2017) by snarfy on Tuesday October 30, 2018 @09:06AM (#18336072)
I like to think of constexpr as a better, typesafe preprocessor. There are a lot of places constexpr can replace it.