Inspired perhaps by STL, C++ programmers have begun to make wide use of generic programming techniques. This has increased the need for improved static (compile-time) checking. Thinking in terms of generic data structures and algorithms, if there are certain conditions which must be satisfied within a program, the programmer must ensure that a template cannot be instantiated for a type that does not satisfy those conditions. A small helper devoted to such a task is gaining popularity in the C++ community. There are many implementations of it; the following one is common:
template <bool> struct static_checker; template <> struct static_checker<true> {};It is used as follows. If you instantiate static_checker with true, nothing happens. If you try to instantiate it with false, a compile-time error (or pre-link time error, depending on what compiler you use) will occur. This is because only the specialization for true is defined. (The first line is only a declaration, it doesn't actually define the template.) "And that's all?" you ask. Yes, that's pretty much it. Yet you can use it for very non-trivial checks, like below:
static_checker<sizeof(int) == 4>(); // code that's dependent upon 4-bytes ints follows template <typename T> class small_vector : public static_checker<sizeof(T) <= 8> { // this vector cannot contain elements with size greater than 8 bytes };There are lots of things you can do with static_checker. You can even enforce interfaces and function signatures with it. C++ has been widely criticized for not being able to express interface constraints in template instantiations. Well, with the help of static_checker, it can, and in a flexible (albeit not very intuitive) way.