Re:Proposal to add constexpr to C [pdf]
by zarzavat on Saturday October 16, 2021 @05:58AM
(#28886729)
It's not the same. e.g.
cannot be constexpr. Consider constexpr as a compile time assertion that a given symbol is available at compile time. If you didn't have the constexpr keyword then you would have have to inspect the definition to determine that every subexpression is constexpr.
[ Parent
| on HN ]
Why not
const int x = const foo(bar(y));
?The compile can (and will) do that at run time, it just won't let the code alter x afterwards (either at compile time or run time)
Re:Proposal to add constexpr to C [pdf]
by SuchAnonMuchWow on Saturday October 16, 2021 @01:53PM
(#28889838)
You should check Zig comptime keyword, which does exactly that. Its the feature from zig I miss the most in C.
That sounds like a subset of EVAL-WHEN[1].
Re:Proposal to add constexpr to C [pdf]
by AlexanderDhoore on Saturday October 16, 2021 @06:02AM
(#28886740)
"inspect the definition"
Yes, that's what I meant with "the compiler can do it".
Re:Proposal to add constexpr to C [pdf]
by zabzonk on Saturday October 16, 2021 @06:12AM
(#28886786)
Not given C and C++ separate file compilation.
Re:Proposal to add constexpr to C [pdf]
by zarzavat on Saturday October 16, 2021 @07:22AM
(#28887099)
That might be OK in an IDE driven language such as Java, but I doubt the neckbeards writing C in a plain text editor would go for that level of implicitness. In C++ it might be more acceptable since you practically need an IDE since C++11 to resolve all the auto type inference.
Re:Proposal to add constexpr to C [pdf]
by foxfluff on Saturday October 16, 2021 @08:08AM
(#28887273)
C neckbeard here. I do fully hope and expect and assume the compiler to fold constants and figure out as much at compile time as it can. This is the kind of optimization that doesn't make the code harder to reason about, even when coding with ed, since the logic and ultimate result of these expressions does not change.
Heavy type inference, overloading, and inheritance are a completely different beast and they make the code a lot more opaque.