by AshamedCaptain · 3y ago · 0 comments
Point 8:
> A core constant expression is any expression whose evaluation would not evaluate any one of the following:
> 8. an expression whose evaluation leads to any form of core language undefined behavior (including signed integer overflow, division by zero, pointer arithmetic outside array bounds, etc).
For example, here is a modification of the original program that does invoke UB, but compiles just fine:
<source>:5:17: error: static assertion expression is not an integral constant expression
static_assert(1024*1024*1024*3 != 0, "UB");
https://godbolt.org/z/Wc591En7E constexpr int foo(int x) {
return 1024*1024*1024*x;
}
int main()
{
int y;
std::cin >> y;
static_assert(foo(1)); //all good
foo(y); //oops, UB if user enters 7
}
https://godbolt.org/z/K8h4Kj99sEdit: small correction so that the numbers are big enough to cause problems...
constexpr requires that the function be callable at compile-time, not that it is so.
Otherwise, what you write is provably correct - UB is not statically decidable in all cases (and depending on the type of UB, not even in a lot of cases).
If your compile time evaluations don't trigger signed integer overflow (or any other UB) does it follow that at runtime you couldn't pass a parameter that would trigger signed overflow?
I mean it's still useful because at least you know your test code is not artificially passing because of some UB makes it look like passing
Source ?I am not sure constexpr give any garanty regarding UB and/or leaks
Can you point to a source for that ? I am not trying to be pendantic, but genuilly curious