Take the 2-minute tour ×

If we use multiple inheritance, slicing will make the addresses to parent objects differ from the address to leaf objects:

struct X {int x};
struct Y {int y};
struct Z : X, Y {int z};

So if we have a Z object z, its address &z will not coincide with the address of its Y parent: static_cast<Y*>(&z) is four bytes higher than &z.

The good thing about static_cast is that it's, well, static, so doesn't take up runtime (compared to dynamic_cast, that is). However, if we have a Z* that points at 0, every cast to a parent should and does yield a null pointer as well. Why does this work and how is it implemented? Does that imply that every single static_cast introduces a branch instruction?

share|improve this question
This works because every object of the type Z is also an object of they type Y. A pointer can base class can implicitly point to objects of derived class. –  Alok Save Jun 9 '12 at 14:30
@Als: I'm talking about the implementation of static_cast. Sure each Z contains a Y but their addresses differ. However, as you may note static_cast<Y*>(zPtr) will 0 if zPtr is 0. –  bitmask Jun 9 '12 at 14:33
"it's, well, static, so doesn't take up runtime" -- huh? You do realise that a static_cast can also convert between integers of different sizes, don't you? That isn't (generally) a no-op at runtime either. –  hvd Jun 9 '12 at 14:36
@hvd: Ah, yes. I was thinking "Compared to dynamic_cast" which requires rtti. –  bitmask Jun 9 '12 at 14:37
Ah, that clears that up. But RTTI isn't always required for dynamic_cast either: at least in GCC, dynamic_cast<void*> works even with fno-rtti. –  hvd Jun 9 '12 at 14:44

1 Answer 1

up vote 5 down vote accepted

Yes, both an implicit conversion from a pointer to a derived class to a pointer to a base class and a static_cast back again must preserve null pointer values. This means that a branch is usually required in the generated code for multiple inheritance cases where the base class address does not coincide with the derived class address.

It's theoretically possible for an implementation to reserve a range of addresses around a 'zero' address to represent null pointers and avoid the branch in this case but it would be at the expense of adding extra checking for comparisons of null pointers.

share|improve this answer
but it would be at the expense of adding extra checking for comparisons of null pointers which is a much more common scenario than static_casting to a parent. –  orlp Jun 9 '12 at 15:01
@nightcracker: Absolutely. I can't imagine a practical implementation ever using this strategy; it's more of a thought exercise. –  Charles Bailey Jun 9 '12 at 15:04

Your Answer


