4 ms·
unpopular opinion, I think constexpr and similar are anti patterns. if you want to memoize values, run a separate script to do that. a computation is not and ca
by 38 2y ago
unpopular opinion, I think constexpr and similar are anti patterns. if you want to memoize values, run a separate script to do that. a computation is not and cannot be constant, this is just some sugar so that people can pretend it is.
- g15jv2dp 2y agoUh? Why couldn't a computation be constant? constexpr auto size = 4; constexpr std::array<int, size> arr = {1, 2, 3, 4}; constexpr auto i = 2; constexpr auto x = arr[i];
- 38 2y agoyou dont have a computation in your code.
- g15jv2dp 2y agoconstexpr auto x = 1+1;
- entelechy0 2y ago[dead]
- MathMonkeyMan 2y agoThe compiler is a series of such scripts. With constexpr/consteval/constinit, templates, and reflection, C++ is slowly converging on hygienic macros. Also "a computation is not and cannot be constant" depends on what is meant precisely by "computation" and "constant." Is "23 * 48" a constant, or is it a computation? What about "2208/2"? What about "1.104e3"? You can define "constant" as "something that is not computed but that exists in the program as an immediate value." In that case you're right but only by definition. And the compiler must still "interpret" the immediate value. It's a slightly fuzzy distinction in my eyes.
- dahart 2y ago> a computation is not and cannot be constant Sure it can - but you know that - a compile time computation can be a run time constant. There’s loads of reasons to want constant values built out of compile-time computation. You haven’t had to write any high performance code? Start with something like constexpr float angle_threshold = 45.0f; constexpr float degToRad = M_PI / 180.0f; constexpr float x_threshold = cosf( angle \* degToRad ); The nice thing about having this be an expression is it’s semantic, and I can tune my angle in degrees, not have some raw number that isn’t adjustable. Maybe I need a small table of these, suddenly running a little code at compile time looks great. Doing it during compilation is a lot nicer than having a script dependency and having to write a build step for it. The bigger issue with constexpr specifically is that it doesn’t actually guarantee compile time evaluation…
- nullc 2y agoyet 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.
- unclad5968 2y agoJust what c++ is missing. More complicated builds.
- 01HNNWZ0MV43FF 2y agoIt's gonna come full hilarious circle when someone announces a C++ build tool written in rust
- _huayra_ 2y agoIt exists, and not just for C++: https://buck2.build/ https://buck2.build/
- deschutes 2y agoThis seems to be saying nothing. constexpr allows you to maintainably express the computation of a constant. This is useful, even if it morally is a script generating that constant it's valuable to not need to drag along a bunch of extra stuff to maintain a program.
- sdsd 2y agoWow, this opinion really is unpopular. I'm glad you shared it, because I learned a lot from the replies! I wonder, after all these rebuttals, do you still hold your same unpopular opinion, or has the Vox Populi dissuaded you?
- rowanG077 2y agoA compiler is exactly that. For example it turns the int literal 4738 into a binary representation.