Search code examples
c++templatesv8

How does the following code work?


    #define TYPE_CHECK(T, S)                                     \
    while (false) {                                              \
      *(static_cast<T* volatile*>(0)) = static_cast<S*>(0);      \
    }

I am reading Google v8's code and found the above macro for type check.

However, I do not understand why it works. while(false) never get executed, right? Can someone explain those lines? Thanks


Solution

  • Yes, but the compiler still performs syntax & semantic checks on the loop contents. So if something is wrong (i.e. the implicit type conversion from S* to T* is illegal, which happens if T is neither S nor a base class of S), compilation fails. Otherwise, the quality of the resulting machine code is not affected since the optimizer will detect the nonreachable code and remove it silently.