by kris-jusiak · 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).
<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...
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).
Source ?I am not sure constexpr give any garanty regarding UB and/or leaks
I bet it's mostly a wash, and the ergonomics of conventional gtest macros look way better to my eye.
Basically the idea is that the test code gets written to a different linker section that your test runner can iterate through, when tests are enabled. This is easy on gcc because it generates automatic constants for the beginning and end of different linker sections. There may be away to do this with clang as well, but I never use clang.
You could of course set up your runtime tests in a similar way, having the ide run them back to back as you are writing code, but it is more complicated, especially if the code is in an intermediate state that it is not fully compilable.
So in the end it is not a huge breakthrough, but having compile time tests is still quite a nice feature.