3 ms·
> I will admit that when I first read about constexpr my first thought was 'why can't the compiler figure this out automatically?'. I would argue that the main
by nuerow 5y ago
> I will admit that when I first read about constexpr my first thought was 'why can't the compiler figure this out automatically?'.
I would argue that the main value proposition of constexpr is not what the compiler can figure out automatically but allowing the developer to express where he wants constexpr and afterwards allow the compiler to verify if that's possible.
It's the difference between a soft "nice-to-have" requirements and a hard "must-have" requirement.
- dataflow 5y ago> I would argue that the main value proposition of constexpr is not what the compiler can figure out automatically but allowing the developer to express where he wants constexpr and afterwards allow the compiler to verify if that's possible. If you want to look at it that way, it only applies to variable declarations at best. Not function declarations, which is the most common case of where it's used (and it completely clutters the code). And honestly, if that was the value proposition, they could've just done it a million other ways. Like think even 'template<auto V> static const auto constant = V' would let you do constant<whatever> and verify it at compile time. Yeah it's C++20 but they could've still done it then or used a different approach, there are lots of ways to address this (attributes, etc.). No need to clutter every function prototype in the language for something that small. Also I think you're thinking more of consteval than constexpr.
- nuerow 5y ago> If you want to look at it that way, it only applies to variable declarations at best. Not function declarations, which is the most common case of where it's used (and it completely clutters the code). Not really. The whole point of constexpr is to state that an expression is constant, thus the value returned by the function is computed at compile time. > And honestly, if that was the value proposition, they could've just done it a million other ways. To me it seems thay annotating a function with constexpr is the clearest and most straight-forward way to go about expressing constant expressions. > Also I think you're thinking more of consteval than constexpr. To each his own, but if the whole point is to represent constant expressions then the constexpr keyword seems to be straight-forward.
- dataflow 5y ago> Not really. The whole point of constexpr is to state that an expression is constant, thus the value returned by the function is computed at compile time. That's not even one point of constexpr, let alone the whole point. Putting constexpr in front of a function declaration does absolutely nothing to the codegen. See for yourself: https://gcc.godbolt.org/z/34h6Tanzn https://gcc.godbolt.org/z/34h6Tanzn Notice that both h() and f() are called despite being constexpr. > To me it seems thay annotating a function with constexpr is the clearest and most straight-forward way to go about expressing constant expressions. Again, it doesn't do what you think (see above). It does absolutely nothing to a function. > To each his own, but if the whole point is to represent constant expressions then the constexpr keyword seems to be straight-forward. This isn't an opinion thing. The consteval literally does what you're imagining constexpr does. Just replace constexpr with consteval in the code above. (I could probably argue that one has little use too, but that's not what I'm arguing here.)
- thezilch 5y agoWhat? https://gcc.godbolt.org/z/EzrWW9ss5 https://gcc.godbolt.org/z/EzrWW9ss5
- deleted 5y ago[deleted]
- dataflow 5y agoYou're seeing the compiler opportunistically make an optimization by taking constexpr as an extra inlining hint. I showed you it's not something you can rely on in my example. You have to realize constexpr only talks about what is allowed to happen, not what is guaranteed to happen. You want consteval for that.
- thezilch 5y agoOf course I want consteval, if a later C++ is available to me, but that doesn't excuse you writing constexpr does absolutely nothing, when it clearly does. It's no surprise your example doesn't fold calls without `-O3` or that one could produce a `constexpr` that doesn't fold to a constant (go ahead and try though :>). But consteval function is also not a literal replacement constexpr. For example, you can't take a pointer to the former. There's a reason they co-exist and are not useless.