How to understand this piece of C ++ code that is designed for type checking?

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?

+4
source share
4 answers

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.

+4
source

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.

+1
source

The code checks to see if S * can be assigned to T *, and force compilation if it cannot. Regarding volatility, I'm not sure.

0
source

If types cannot be selected, it will not compile. But it is never executed.

-1
source

All Articles