News for hackers. Stuff that matters.
Re:Proposal to add constexpr to C [pdf] by secondcoming on Saturday October 16, 2021 @06:20AM (#28886812)
Is it superfluous? I will admit that when I first read about constexpr my first thought was 'why can't the compiler figure this out automatically?'. I assume that compiler vendors all agreed that a new keyword was necessary, they don't add them without reason.
Re:Proposal to add constexpr to C [pdf] by nuerow on Saturday October 16, 2021 @07:11AM (#28887055)
> 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.

Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @11:26AM (#28888505)
> 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.

Re:Proposal to add constexpr to C [pdf] by nuerow on Saturday October 16, 2021 @01:33PM (#28889607)
> 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.

Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @02:53PM (#28890373)
> 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

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.)

Re:Proposal to add constexpr to C [pdf] by thezilch on Saturday October 16, 2021 @03:47PM (#28890847)
Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @03:59PM (#28890939)
You'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.
Re:Proposal to add constexpr to C [pdf] by thezilch on Saturday October 16, 2021 @04:26PM (#28891167)
Of 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.

Re:Proposal to add constexpr to C [pdf] by dataflow on Saturday October 16, 2021 @04:35PM (#28891241)
> Of 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.

consteval wasn't intended to be excusing anything. I was just letting you know you're misunderstanding what constexpr does, and that what you're thinking it's supposed to do is in fact consteval's job.

You seem very confused about language specifications. That GCC and Clang happen to interpret constexpr as an extra "please inline" hint under -O3 is not evidence of constexpr "doing" something, and certainly not justification for including it in the language standard. That's like saying HN's orange color is "doing something" just because you really like to go eat an orange every time you see it.

I suspect you will have a hard time finding any evidence that constexpr was ever intended to be a "this is technically optional but please really, really try to inline this" hint of sorts to the compiler by the committee.

> But consteval function is also not a literal replacement constexpr.

I didn't say it was.

> For example, you can't take a pointer to the former.

"Therefore constexpr is useful" does not follow from this.

> There's a reason they co-exist and are not useless.

There's a mediocre reason for consteval, and a bad reason for constexpr. The former is probably unnecessary IMO though still of marginal utility, and in any case I can concede there's a debate that could be had there. The latter would be practically useless if the language cared to just make it the implicit default. It's really only solving a problem it created.

If you're using constexpr as an "optional but highly recommended inline" hint like you're showing me in your examples, you're doing the equivalent of pulling up HN's orange color so it coaxes you (or someone else) to go eat an orange. You are certainly welcome do that (hopefully it's a free country where you live) and I'm not stopping you per se, but it's missing both the letter and the spirit of the keyword(/color) by a colossal margin.

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Saturday October 16, 2021 @07:53AM (#28887202)
The compiler can figure it out. But that doesn't solve the problem constexpr was created for. Without constexpr compile time evaluation becomes very brittle. Constexpr is there to help the compiler and developer reach an understanding of the stuff that has to be evaluated at compile time.
Re:Proposal to add constexpr to C [pdf] by atq2119 on Sunday October 17, 2021 @05:30AM (#28895123)
The problem is that (especially library) code just becomes absolutely littered with constexpr, because realistically speaking, you do want everything that can be constexpr to actually be constexpr.
Re:Proposal to add constexpr to C [pdf] by rowanG077 on Sunday October 17, 2021 @06:07AM (#28895293)
Why is that a problem? I actually really like seeing if a library function can be cte'd.
Re:Proposal to add constexpr to C [pdf] by dataflow on Sunday October 17, 2021 @03:16PM (#28898943)
> Why is that a problem?

Because whether a call should be CTE'd is a decision for the caller to make, not the callee.

> I actually really like seeing if a library function can be cte'd.

That's not what you're seeing with constexpr. It's what you're seeing with consteval. [1]

What you're seeing with constexpr is which functions are prohibited from being CTE'd. Which is pretty useless when you don't intend to CTE them in the first place, and actively counterproductive when you do.

[1] Actually this is kind of a lie too [2], but at least it's much closer to the truth.

[2] Notice that seeing consteval here tells you absolutely nothing: https://gcc.godbolt.org/z/bWzd4v6Y1

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Monday October 18, 2021 @08:05AM (#28904551)
consteval says it MUST be constant evaluated not that it CAN be constant evaluated. That is very different. With consteval it's not up to the caller to make the decision of whether to run at compile-time or not, the caller must provide constexpr arguments. With just a constexpr annotation the caller can decide to run it either at compile-time or run-time.

See this code:

https://godbolt.org/z/oMWKcWW5e

Re:Proposal to add constexpr to C [pdf] by dataflow on Monday October 18, 2021 @08:09AM (#28904578)
Like I said:

>> [1] Actually this is kind of a lie too [2], but at least it's much closer to the truth.

>> [2] Notice that seeing consteval here tells you absolutely nothing: https://gcc.godbolt.org/z/bWzd4v6Y1

Note I didn't claim this was their only difference.

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Monday October 18, 2021 @08:29AM (#28904698)
I don't see what's your problem with libraries marking their functions as constexpr then. The library says it's up to the caller to decide whether this function is constant evaluated. Precisely what you wanted. If libraries start marking their function as consteval. Now that would be a problem.
Re:Proposal to add constexpr to C [pdf] by dataflow on Monday October 18, 2021 @08:33AM (#28904722)
> I don't see what's your problem with libraries marking their functions as constexpr then. The library says it's up to the caller to decide whether this function is constant evaluated.

The problem was explained here: https://news.ycombinator.com/item?id=28895123

The language could just say everything that could be constexpr is implicitly constexpr, and you'd get just as useful of an effect (even on existing code!) without anyone having to do anything extra. It's very rare for a function that could be constexpr to be justifiably prohibited from being used as such, and they could've introduced an opt-out attribute for that if they really cared.

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Monday October 18, 2021 @08:53AM (#28904898)
You are going in circles...

constexpr is an interface guarantee. If it were not there the entire optimization stack would become brittle. Imagine updating a dependency or rewriting a function and suddenly your program is 10x slower. Things like this are known in other languages. For example Haskell and fusion optimization. It's fine for you to disagree with this reasoning. But then you should not be using C++ as you don't really care for performance and can use many other better suited languages.

Then you say constexpr doesn't make it possible for the caller to specify whether something is cte'd while this is precisely what the caller decides. You are confusing constexpr and consteval. consteval doesn't make it possible for the caller to decide whether the function is cte'd.

Please answer the points I have brought up instead of simply referring to something I have already refuted.

Re:Proposal to add constexpr to C [pdf] by dataflow on Monday October 18, 2021 @08:59AM (#28904951)
> Imagine updating a dependency or rewriting a function and suddenly your program is 10x slower.

That can already happen and will still be possible after constexpr. And it was always preventable by forcing evaluation of the variable in a CTE context, like in std::integral_constant. And you're missing the fact that an implicit constexpr with an explicit opt-out needn't behave any differently from one with explicit opt-in. Same semantics, just fewer keystrokes.

> constexpr is an interface guarantee

> Please answer the points I have brought up instead of simply referring to something I have already refuted.

You didn't actually refute it (though you tried), and I already did address them:

>> [2] Notice that seeing consteval here tells you absolutely nothing: https://gcc.godbolt.org/z/bWzd4v6Y1

Replace consteval with constexpr and you'll see it told you absolutely nothing about bar()'s CTE'ability. Observe how the callee used constexpr and bar() failed to CTE despite being constexpr: https://gcc.godbolt.org/z/odqbT7x9j

> You are going in circles...

Because you went in circles. I've directly addressed every single one of your points but then you looped back to ask the same question as you did in the beginning. Same questions, same answers.

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Monday October 18, 2021 @09:21AM (#28905165)
> [2] Notice that seeing consteval here tells you absolutely nothing: https://gcc.godbolt.org/z/bWzd4v6Y1

In that example marking bar as consteval does nothing. Because you are not ever calling bar... Calling bar shows that it's not possible because foo is not constexpr:

  note: non-constexpr function 'foo<int>' cannot be used in a constant expression
  consteval T bar(T x) { return foo(x); }
Marking foo as constexpr makes it compile and compile time evaluate both foo and bar.

> Replace consteval with constexpr and you'll see it told you absolutely nothing about bar()'s CTE'ability. Observe how the callee used constexpr and bar() failed to CTE despite being constexpr: https://gcc.godbolt.org/z/odqbT7x9j

It doesn't compile because foo is not constexpr. Marking foo as constexpr makes it compile and foo and bar are evaluated at compile time. It produces this ASM:

  main:                                   # @main
          pushq   %rbp
          movq    %rsp, %rbp
          movl    $0, -4(%rbp)
          movl    $0, -8(%rbp)
          xorl    %eax, %eax
          popq    %rbp
          retq
At least try to the examples you are using to make your point. Because they in fact show that you are wrong...
Re:Proposal to add constexpr to C [pdf] by dataflow on Monday October 18, 2021 @09:55AM (#28905467)
The examples are not wrong, you're just not understanding them.

> It doesn't compile because foo is not constexpr.

Well obviously. That's not the question here. The question is, explain to me exactly what guarantee the declaring bar() as 'constexpr' achieved for anyone? It clearly didn't prevent its author from calling a non constexpr function, and it clearly didn't signal to its users that it is usable from a constexpr context either. The user had to go inspect each one of its callees manually to see if they could all be evaluated at compile time in reality, which is exactly what you just did with foo(). Which is pretty damn hard (as in, Turing-undecidably-hard) when things are templates and you have ADL on top of that. And which the presence of constexpr on bar() didn't help you one bit with.

The declaration of bar() told you exactly nothing by having constexpr in it. It made no difference whatsoever for itself, nor for main(). It just added clutter.

Re:Proposal to add constexpr to C [pdf] by rowanG077 on Monday October 18, 2021 @10:33AM (#28905818)
The code doesn't compile, so it did in fact stop the author from calling a non-constexpr function from a constexpr function. You can't meaningfully semantically talk about code that doesn't compile.
Re:Proposal to add constexpr to C [pdf] by dataflow on Monday October 18, 2021 @01:07PM (#28907831)
> You can't meaningfully semantically talk about code that doesn't compile.

You can point out that it failed to compile, and that can be a benefit when compiling would've been detrimental. Just like you yourself literally just did when you said "The code doesn't compile [...]".

> it did in fact stop the author from calling a non-constexpr function from a constexpr function.

The constexpr in front of bar() is not what prevented compilation. You don't believe me? Just take it out and see. The code would've failed to compile the exact same way without constexpr in front of bar(). Try it before disagreeing with me.

You're saying putting constexpr in front of bar() has a benefit. I'm saying: tell me what it was. Making that constexpr be always in front of bar() implicitly would've resulted in exactly the same outcome in every situation where the code compiles and also where it fails to compile (like above). Nothing would've been any different. I don't get why this is so difficult but I'm not going to keep repeating myself. Everything I can possibly imagine explaining is in my comments up to this point so just re-read them, I have no other way I can phrase these. Everyone else has understood what I'm saying and I'm at a loss how else to explain it differently. I promise you it's not wrong, we're just failing to communicate.

Re:Proposal to add constexpr to C [pdf] by pjmlp on Saturday October 16, 2021 @06:27AM (#28886861)
It can, check the C++ compiler Circle.

https://www.circle-lang.org

Unfortunately it isn't a direction WG21 is willing to adopt.

Re:Proposal to add constexpr to C [pdf] by wtetzner on Saturday October 16, 2021 @09:49AM (#28887865)
I just assumed the constexpr keyword was added to help the programmer ensure that what they were writing could be run at compile time.