Use C_ASSERT, a macro supplied by the Windows headers, to specify a compile-time assert. A standard assert causes break in the execution when the specified condition is not met. A compile-time assert does the same thing but the condition is evaluated at compile time which means that it can be caught before the code is checked in. A compile-time assert can only be used when the value in question is available at compile time. It is useful for keeping static tables in source in sync. For instance, you may have two static arrays in source that need have the same number of elements. When the programmer adds a new entry to one list, they need to add an entry in the other list. If they don’t, then bad things will happen at runtime. Rather than supplying code to handle this error condition at runtime, a compile-time assert can be used to compare the sizes of the two tables and assert that they are the same. If the programmer increases the size of one array and not the other, they will not be able to compile their code.

This page is an example of Catching defects with Patterns.

Community content is available under CC-BY-SA unless otherwise noted.