During Sunday's tutorial (29-May-05) we had a discussion regarding the differences between static_cast and reinterpret_cast. This is a simplified version of the question that triggered the discussion:
"Suppose you have to translate the following C program into C++:

struct A { ... };
void f(void* p) { struct A* a = (struct A*) p; ... }


Which of C++'s cast operators would you use? (a) dynamic_cast (b) const_cast (c) static_cast (d) reinterpret_cast"

Obviously, reinterpret_cast<A*>(p) can be used to carry out this type of casting. Nonetheless, it turns out that static_cast<A*>(p) will work just as well. In a nut shell, static_cast can convert between two "related types" as long as it does not cast away constness. Two types T,R are considered to be related if there is a "natural" coversion either from T to R or from R to T. (See also the full specifications of static_cast)

Consequensly, static_cast can be used in situations such as:

In particular, static_cast is a valid answer to the question above. Moreover, given that we would generally prefer static_cast over reinterpret_cast, we end up with static_cast being the correct answer.

Note that this behavior of static_cast means that static_cast is more powerful - and therfore, not as safe - as most people (including me) think.

-Itay