Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the following code work?

Tags:

    #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

like image 319
Jexcy Avatar asked Mar 08 '11 19:03

Jexcy


People also ask

WHAT IS AN code?

Code (short for source code) is a term used to describe text that is written using the protocol of a particular language by a computer programmer. Examples of programming languages include C, C#, C++, Java, Perl, and PHP.

What is an example of coding?

Here's a simple example of code, written in the Python language: print 'Hello, world!' Many coding tutorials use that command as their very first example, because it's one of the simplest examples of code you can have – it 'prints' (displays) the text 'Hello, world! ' onto the screen.


1 Answers

Quite a fancy hack - the purpose of the macro seems to be to check if the type S is assignable to (i.e., is a subclass of) the type T. If it is not, the pointer cast from S* to T* will produce a compiler error. The while (false) prevents the code from actually having any other effect.

like image 196
Aasmund Eldhuset Avatar answered Oct 04 '22 07:10

Aasmund Eldhuset