What's wrong with static polymorphism that have been around for at least 31 years? (1995 per [1]). Also know as CRTP [2] Apart, of course, from ugly verbose compilation errors...
And now c++23 "deducing `this`" [3,4] modern replacement which is supposed to improve compilation time and remove the verbose errors.
[1] https://www.artima.com/articles/the-most-important-c-non-boo...
[2] https://en.cppreference.com/cpp/language/crtp
[3] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p08...
[4] https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/
By whole program analysis I mean you need the entire call tree of a function - stopping only when you can validate that the range of values is constrained to legal values. For some functions this is really simple, while for others the whole flow can be thousands of functions.
Take the example from the article. The contract trips at runtime, not at compile time.
There is a lot of interest in static analysis enforcing contracts, but this will likely require some additions to the contract specification.
> this will likely require some additions to the contract specification.
You need a lot more machinery to move this stuff to compile time, and not everything can be checked at compile time. The first example in the post would require you to validate that a <= v's len before indexing in order to be evaluated at compile time, for example. (Which of course is a runtime check anyway...)
Also take a look at David Crocker's Escher Technologies "Escher C Verifier and Escher C++ Verifier" (https://www.eschertech.com/products/ecv.php) and articles on "Verified Design-By-Contract" etc. linked to in my comment chain here - https://news.ycombinator.com/item?id=48544022
Gcc can already do link-time devirtualization in some cases.