Consider the function:
constexpr int myfunc(int a) { if (a < 10) return -a; else return a*a; }
Now, right under it I can do:
static_assert(myfunc(10) == 100, "test myfunc: edge case when input is 10");
Consider, all the tests that can now be done at compile time. Now it is easier for a programmer to break the build! It is now obliged for a developer to run the tests on their machine before a submit! No modifications to the compiler needed. Even Xcode will underline static-asserts whose condition is violated as you type.
This little gem does have some caveats, though. First, you can't debug the function while it is compiling, so kiss goodbye to the debugger until you comment out the unit tests, call the function with a value unknown at compile time (like input from the user) and then once that is done, re-enable the tests (because of this, you want to write many small functions to combine and test in parts for ease of development). Second, it only works if, as we pray is the case, that the compiler executes code as identically as possible as to what the compiled code would. Third, we are limited to deterministic functional cases which limits the utility of the method.
No comments:
Post a Comment