Possible duplicate:How does the following code work?
#define TYPE_CHECK(T, S) \ while (false) { \ *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \ }
It seemed complicated to me how it works? And why are volatiles?
Since this is a static cast, it gets the compiler to make sure that pointers to two types are equivalent. while(false) ensures that it will never be run and that it is just compilation time.
while(false)
Statically checks that a S* can be added to T* . At runtime, the code is not executed. I think volatile prevents optimization, which simply causes the compiler to ignore the code.
S*
T*
volatile
The code checks to see if S * can be assigned to T *, and force compilation if it cannot. Regarding volatility, I'm not sure.
If types cannot be selected, it will not compile. But it is never executed.