News for hackers. Stuff that matters.
Re:Proposal to add constexpr to C [pdf] by AlexanderDhoore on Saturday October 16, 2021 @05:49AM (#28886681)
Do you mean that C++ could have done without `constexpr` and could have used the existing `const`? Because that has been my feeling for a very long time. Why not expand the meaning of const in certain contexts. Compilers know if something can be evaluated at compile-time. No need for me to tell them explicitly.
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.

   const int x = readInput();
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.
Re:Proposal to add constexpr to C [pdf] by nine_k on Saturday October 16, 2021 @06:20AM (#28886817)
Why not

  const int x = const foo(bar(y));

?
Re:Proposal to add constexpr to C [pdf] by mpyne on Saturday October 16, 2021 @01:57PM (#28889877)
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.
Re:Proposal to add constexpr to C [pdf] by User23 on Saturday October 16, 2021 @04:25PM (#28891162)
That sounds like a subset of EVAL-WHEN[1].

[1] http://clhs.lisp.se/Body/s_eval_w.htm

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.

Re:Proposal to add constexpr to C [pdf] by pjmlp on Saturday October 16, 2021 @06:25AM (#28886850)
Yes, all the constsomething is superfluous, as proven by Circle.

https://www.circle-lang.org/

Unfortunately, there is a big political war ongoing, so instead C++ keeps collecting const.... keywords.

Re:Proposal to add constexpr to C [pdf] by voldacar on Saturday October 16, 2021 @08:11AM (#28887297)
"Constexpr" is possibly the most hideous keyword in any programming language. My eyes vomit a little each time I read it.

They could have just used "pure" like in D, but this is C++, so that would be too sane and obvious.

Re:Proposal to add constexpr to C [pdf] by hmfrh on Saturday October 16, 2021 @08:26AM (#28887370)
Introducing a new keyword means that all existing code that uses the new keyword as an identifier will break. That's why the `constexpr` keyword is deliberately weird.

It's also why they reuse keywords like `using`.

Re:Proposal to add constexpr to C [pdf] by kevin_thibedeau on Saturday October 16, 2021 @02:50PM (#28890348)
They could just use the reserved keyword naming like the C committee does.
Re:Proposal to add constexpr to C [pdf] by pjmlp on Saturday October 16, 2021 @08:17AM (#28887325)
Politics, check the outcome of the networking vote.
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @10:58AM (#28888296)
No, not all const is superfluous. You don't want const-able method to be const. And const is a property of the declaration, not the definition. But constexpr is superfluous. It's something the compiler can infer purely from looking at the definition, and it has little to no "design" value like const does.
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @03:27PM (#28890685)
> You don't want const-able method to be const.

Edit, typo: I meant "You don't want every const-able method to be const."

Re:Proposal to add constexpr to C [pdf] by nly on Saturday October 16, 2021 @07:56AM (#28887223)
I can't confirm in this specific case but, more often than not, the reason for these redundancies is a fraction of the committee felt uncomfortable with the implicit solution, so the keyword got added.
Re:Proposal to add constexpr to C [pdf] by pjmlp on Saturday October 16, 2021 @08:16AM (#28887322)
There is a video about the story background, where a group of kids play the role of the committee voting down Circle.
Re:Proposal to add constexpr to C [pdf] by nly on Saturday October 16, 2021 @02:48PM (#28890335)
Link?
Re:Proposal to add constexpr to C [pdf] by pjmlp on Saturday October 16, 2021 @03:54PM (#28890903)
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @10:56AM (#28888280)
Basically, yeah.

You don't even need 'const' for this. Under the as-if rule, subsets of the program could always already be evaluated at compile time and have the result substituted in the program. Compilers already did that when they had the definition handy. Like just do int x = f(5); and then define int f(int x) { return x * 2; } and you'll get int x = 10; in any compiler without any keywords whatsoever. The constexpr keyword just cluttered the code for no good reason.

Re:Proposal to add constexpr to C [pdf] by NavinF on Saturday October 16, 2021 @11:25AM (#28888502)
In theory compilers can always optimize constant expressions, but they don't IRL. See my example above where I multiply the elements of an array containing {2,2}: https://news.ycombinator.com/item?id=28886999

My code is C style and unrealistically simple, yet the compiler can't even constant-fold that.

If you've looked at the output of real compilers, you'd know that it's unrealistic for a language to let you to use the result of an arbitrary function call where you'd normally use a constant.

Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @11:30AM (#28888529)
I'd already replied to you on that example and explained this has nothing to do with optimizations as far as performance and mechanics go. The only relevance of optimizations is to show that the language semantics always permitted it. Not to suggest an optimize needs to be run to actually do this.
Re:Proposal to add constexpr to C [pdf] by comex on Saturday October 16, 2021 @03:04PM (#28890470)
It depends.

For one thing, constexpr guarantees that errors will produce compilation failures. (At least it guarantees that when used in certain ways; unfortunately C++ overcomplicates things.) In a version of C that doesn't have an explicit constexpr keyword but does allow the compiler to treat arbitrary variables as constants, you could perhaps approximate this by using a value in a context that forces it to be a constant expression… e.g. `const int x = foo(); _Static_assert(x == x);`. But that's a hack. Much better to have an explicit way to say "please evaluate this at compile time".

Also, suppose the user doesn't use an expression in a way that forces it to be evaluated at compile time, but you still want to allow the compiler to do constant evaluation for the sake of faster runtime performance or smaller code size. In this example, should fib(35) be evaluated at compile time or runtime?

    unsigned int fib(int n) {
        return n <= 1 ? 1u : fib(n - 1) + fib(n - 2);
    }

    int main() {
        fib(26);
    }
It depends on the user's intent. In this case, evaluating fib(35) takes less than a millisecond at runtime on my computer, but evaluating it as constexpr with clang requires about a full second. (After all, naive fib performs an exponential number of recursive calls.) Sounds like it's probably a job for runtime… unless the user really values runtime performance or code size more than compile times (maybe they're targeting a really slow CPU), in which case they might prefer it to be done at compile time. Best to give them a choice.

Making things worse, the compiler has no real way of knowing how long something will take to constant-evaluate other than trying and seeing. It can give up after a timeout, but if the timeout isn't very tiny, it risks wasting a lot of time on aborted constant evaluation attempts. Therefore, while optimizers already try to perform opportunistic constant folding, they have to be very conservative with it. This problem doesn't occur if the compiler knows that something should be constant-evaluated because the user has marked it as such.

Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @03:14PM (#28890565)
> In this example, should fib(35) be evaluated at compile time or runtime?

In that example, at compile time. If you write fib(35) you deserve to have to wait for the compiler to do it at compile time instead of at run time. (I'm not saying this to be funny. I'm really trying to make a point. You really do need a realistic example to motivate your side of the argument. If they're hard to come up with, that's kind of my point.) And honestly if I wrote that I would fully expect my compiler's optimizer to already waste that time constant-folding anyway; if I was concerned about its compile time I wouldn't write that even without constexpr.

> Best to give them a choice.

A need for that choice (assuming it's even necessary here, which is debatable, see above) doesn't mean you need a new keyword, especially not for functions. It could've been an attribute, and whatever it is, it only really makes sense on variables, not functions. And whatever it is, turning it into an exclusion instead of an inclusion would probably make sense here, seeing as how so much of the standard library has constexpr in front of it.

Re:Proposal to add constexpr to C [pdf] by bobthebuilders on Saturday October 16, 2021 @07:12PM (#28892419)
What? Why would fib(35) be evaluated at comple time? What if it's fib(1500)? The computers I want to run the program on is way faster than the pi I use for development.
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @07:18PM (#28892451)
> What? Why would fib(35) be evaluated at compile time?

Same reason why fib(2) might be.

> What if it's fib(1500)?

Are you familiar with exponential growth? This would take longer than the heat death of the universe, whether you evaluate it at compile time or run time...

Re:Proposal to add constexpr to C [pdf] by bobthebuilders on Sunday October 17, 2021 @03:17PM (#28898961)
Right, but maybe a military supercomputer could compute such a thing. I know my pi surely can't.
Re:Proposal to add constexpr to C [pdf] by electroly on Saturday October 16, 2021 @02:46PM (#28890318)
If nothing else, constexpr as a keyword lets you force it to happen and fail if it can't happen, rather than silently reverting back to runtime execution. I don't think it's certain that C++ will remove the keyword.
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @02:56PM (#28890403)
constexpr on a variable declaration does that (i.e. it's still superfluous on function declarations, which is where it clutters code by far the most). But even for that, there was already std::integral_constant which would've forced this, and they could've just updated it to C++20 via template<auto Value> static const auto constant = Value; if they really wanted to. Or added an [[attribute]] for variable declarations, just like how we have [[likely]] etc. No need to introduce a whole new keyword for this, especially not in a ton of other contexts.
Re:Proposal to add constexpr to C [pdf] by electroly on Saturday October 16, 2021 @03:29PM (#28890699)
Those suggestions, honestly, don't sound better than the keyword to me. I think you're taking it as a given that if a keyword can be avoided, it must be avoided, but I don't fundamentally agree with that at all.
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @03:37PM (#28890763)
I mean your (only?) argument for the constexpr keyword at this point is "it can be a keyword and I feel like it should be, even when I acknowledge it's redundant" then by all means go ahead. Personally I don't think that's a good way to do language design, but I can't argue with subjective feelings and aesthetics and I was never intending to. I'm pretty sure that's not one of the bases on which it made it into the standard though, and those are what I'm trying to address.