Once reading v8.h in V8 engine code, I could find the following macro.
#define TYPE_CHECK(T, S) \
while (false) { \
*(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
}
I know that this is to check the type S is compatible with the type T. In the statement, how can the execution flow enter the while loop? while(false) means that condition is always false. Thus, the statement in while loop will never be executed.
As a result, the macro is not always usable, is it?
The macro is always usable. The intent is to produce a compile-time error or warning (namely that one type is not compatible with another).
The purpose of wrapping it in
while (false)is to prevent the code ever executing at runtime – and with modern compilers the code probably never makes it in to the final binary (optimised out).If you want to know more about this technique, read up on static assertions.