News for hackers. Stuff that matters.
Re:C++17 constexpr everything, or as much as the compiler can by mehrdadn on Thursday December 28, 2017 @03:01PM (#16024474)
I have yet to understand what information 'constexpr' conveys to the compiler that makes it necessary or useful.
Re:C++17 constexpr everything, or as much as the compiler can by minipci1321 on Thursday December 28, 2017 @03:40PM (#16024735)
Certain things, when used in a routine, make computation impossible at compile time. If the routine is marked with 'constexpr', the compiler will verify that.
Re:C++17 constexpr everything, or as much as the compiler can by mehrdadn on Thursday December 28, 2017 @04:31PM (#16025044)
Couldn't it already do the exact same thing without constexpr? (And shouldn't it have already done that when optimizing? In fact for simpler expressions compilers already do this, right?) How does specifying constexpr help?
Re:C++17 constexpr everything, or as much as the compiler can by minipci1321 on Thursday December 28, 2017 @04:42PM (#16025136)
But I think you nailed it -- the compiler doesn't have to signal optimization "failures" back to the developer, but it has to for the constexpr case. It is not that the constexpr routine can be used at compile time, it is that it must be useable at compile time.
Re:C++17 constexpr everything, or as much as the compiler can by mehrdadn on Thursday December 28, 2017 @04:57PM (#16025253)
> But I think you nailed it -- the compiler doesn't have to signal optimization "failures" back to the developer, but it has to for the constexpr case.

Is this true? Where do you see Clang emit a diagnostic in the example in the given article? (https://godbolt.org/g/HKcPFT)

Re:C++17 constexpr everything, or as much as the compiler can by minipci1321 on Thursday December 28, 2017 @05:58PM (#16025824)
You seem to be right -- at least "no diagnostics required" is mentioned a few times in $10.1.5 of N4700. To be honest, my comment is not from the article in the example, but from my own experience, and that is mostly with GCC 7.2.
Re:C++17 constexpr everything, or as much as the compiler can by mwkaufma on Thursday December 28, 2017 @06:28PM (#16026037)
Like "const", it's so the compiler can produce better feedback for the programmer, not better executables. constexpr will raise an error if a function _can't_ be evaluated at compile-time, just as const functions will raise an error if they do any non-const stuff.
Re:C++17 constexpr everything, or as much as the compiler can by mehrdadn on Thursday December 28, 2017 @06:46PM (#16026181)
> Like "const", it's so the compiler can produce better feedback for the programmer, not better executables. constexpr will raise an error if a function _can't_ be evaluated at compile-time, just as const functions will raise an error if they do any non-const stuff.

But how does it lead to better feedback? constexpr is purely suggesting an optimization that the compiler was already allowed to do anyway, and which it can still refuse to perform even with the keyword.

If the programmer's goal is to ensure compile-time evaluation is guaranteed, constexpr won't cut it, since the compiler can (and compilers currently do) silently fall back to run-time evaluation.

Alternatively, if the programmer's goal is to ensure compile-time evaluation is possible, constexpr still doesn't provide any value, since that's already obvious from the compiler analyzing the body of the function and noticing, say, that fopen() is getting called (and the compiler already has to do this anyway).

The situation is very critically different from const, too. Adding 'const' to a method that was previously non-const, even when it is legal and compiles perfectly fine, can entirely change the semantics of the code, and hence you want that decision to be explicit, not implicit. This isn't the case with constexpr.

Re:C++17 constexpr everything, or as much as the compiler can by mwkaufma on Thursday December 28, 2017 @11:08PM (#16027448)
>> But how does it lead to better feedback?

Because you can use static_asset (only accepts constexprs) and in C++17 static-if (a first class cousin to #if)

Re:C++17 constexpr everything, or as much as the compiler can by Too on Friday December 29, 2017 @03:09AM (#16028336)
Add std::array size initialization to that list, as demonstrated in the article.