3 ms·
This is both ingenious and terrifying... The underlying “paradox” seems to be that a constexpr is in spirit a compile-time constant, that testing whether certa
by Chris_Newton 11y ago
This is both ingenious and terrifying...
The underlying “paradox” seems to be that a constexpr is in spirit a compile-time constant, that testing whether certain things are defined is possible using such an expression, but that in C++ whether those things have in fact been defined yet changes during the course of compilation.
So, if you can control exactly when things become defined, for example by using clever template instantiation mechanics as shown here, you can potentially change the value of a constexpr during the course of compilation.
I could see this having some entertaining and perhaps useful possibilities for people who enjoy things like template metaprogramming. On the other hand, the robustness of the technique does seem to depend on being able to very tightly control those instantiation mechanics, which is a minefield already in C++, which I suspect makes this technique a little too clever for production use.
[Edit: Removed reference to this technique being like embedding a compile-time linear type system. That doesn’t seem to be quite the right analogy here; it’s more a system where you can generate unique new values but never return to values you had before once they have changed.]