3 ms·
yet any remotely competent compiler would have turned degToRad to a constant regardless.
by nullc 2y ago
yet any remotely competent compiler would have turned degToRad to a constant regardless.
- biorach 2y agoThat's a rather dickish way of completely missing the point
- nullc 2y agoI apologize for offending, it certainly wasn't the intent. But I don't believe one can justify the existence of constexpr simply by pointing to code that absolutely any compiler would make into constants absent it. Perhaps in your example one could say that the value is that you really want the compilation to abort should changes happen to the code that make it impossible for the compiler to turn it into constants, since doing so is performance critical-- I think that would be a fair point.
- dahart 2y agoSounds like you’re close to justifying the existence of constexpr, that sounds similar to the summary of what it’s for. I wasn’t trying to justify the existence of constexpr, I was justifying the existence of compile time constant computation in response to what @38_14 said about there being no such thing as a computation that’s constant, which is, you probably agree, a bit silly. The top comment didn’t seem to be talking about constexpr specifically since the point was about computation and they said “constexpr and similar” which I assume includes const, consteval, as well as the general idea of baking the results of compute at compile time. HN isn’t really the right place to demand justifying the existence of constexpr, but its existence has been debated at length by people who know a lot more about C++ than me. If you want to read about why it exists, there are lots of good places like cppreference.com and isocpp.org and even stack overflow.
- dahart 2y agodegToRad wasn’t the point, the list of cosines being computed at compile time was the point… or something similar, or something different… use your imagination. I used cosf() because it’s clearly a function call doing compile time compute, and I suggested a list because it makes a more clear case for compile time compilation, and because that might start to get into the reasons one would want constexpr allocation. And yes of course a good compiler absolutely will turn things into constants if it can, regardless of how they’re declared, including without any special const* specifiers at all. That is not why either const or constexpr exist, right?