Rico Wang
Rico Wang

Reputation: 89

static_cast<T* volatile*> - what does this code mean?

In V8.h of Google V8 Javascript engine, there is a piece of code to check if two types match at compiling stage. I can understand large part of it, but can't comprehend the syntax of static_cast<T* volatile*>, what does it mean by adding unusual volatile* and why is it needed?

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

I noted same code has been discussed in this topic below, but not in detail of the question I am asking. How does the following code work?

Upvotes: 1

Views: 273

Answers (1)

Brian Bi
Brian Bi

Reputation: 119219

T* volatile* means "pointer to volatile pointer to T". So it is the same as T**, except that when dereferenced, the resulting lvalue is volatile.

As for why volatile is needed here, that's explained in the commit description, which you can view here: https://github.com/v8/v8/commit/35a80e16241308b4f476875d0f96282cf697a029

TYPE_CHECK in v8.h should assign to volatile qualified null-pointer.

Unless the pointer is volatile qualified, Clang will warn that LLVM removes the assignment during optimization. This is not a problem as that code should never execute, but the warning is treated as an error when building Chromium, and thus stops the build.

Upvotes: 6

Related Questions