8

I know that it's legal to use dynamic_cast to do a "cross-cast" across a class hierarchy. For example, if I have classes that look like this:

  A   B
   \ /
    C

If I have an A* pointer that's pointing at an object of type C, then I can use

A* aPtr = /* ... something that produces a C* ... */
B* bPtr = dynamic_cast<B*>(aPtr);

to get a pointer to the B base object of the C I'm pointing at.

The reason I mention this is that at the time that I write the above code, it's possible that the compiler has not yet seen the definition of C even though it's seen A and B. This means that it's possible that the compiler does not detect any sort of connection between A and B, but it still has to compile the code anyway because it's possible for a class like C to exist and for the dynamic_cast to succeed under some circumstance.

The problem is that this means that I can accidentally cross-cast to an object of the wrong type. Suppose that I have classes that look like this:

A   B    D
 \ /   
  C

Here, D is some random unrelated class. If I try writing something like this:

A* aPtr = /* ... get a C* pointer ... */
D* dPtr = dynamic_cast<D*>(aPtr);

Then this dynamic_cast will always fail at runtime, since there's no possible way to connect A and D. If I'm using D accidentally because I meant to use B, the compiler will give me no indication whatsoever that I have a meaningless cast.

My question is: is there some way that I can get the compiler to warn me that the cast will always fail at runtime? I'd be happy with a language-level solution or some compiler setting for any major compiler that could detect this. If there's an external tool, that's fine as well; I just want to know if it's possible to catch this class of errors.

4
  • This would need to be linker level surely? At compile time, you don't know a class might not be created that derives from A and D?
    – Keith
    Mar 16, 2011 at 6:14
  • @Keith- Definitely, unless you have an omniscient compiler. Man, I want one of those... :-) Mar 16, 2011 at 6:15
  • @Keith: Even the linker may not have full information about all types, as explained in my answer.
    – Ben Voigt
    Mar 16, 2011 at 6:18
  • @Ben Voigt- Absolutely, occurred to me too.
    – Keith
    Mar 16, 2011 at 22:39

3 Answers 3

3

It's not possible to detect this at compile-time. The class C that introduces the relationship could be found in a dynamically loadable library that hasn't even been written yet, and the compiler can't prove otherwise.

There may be a few exceptions though. If A has only private constructors (or a private destructor) then the compiler can be certain that there will be no new subclasses that aren't named as friends by A.

2
  • That's definitely true. However, would it be possible to somehow get the linker to warn me something to the effect of "hey, just letting you know that this looks suspicious?" even if it may not end up being a problem? Mar 16, 2011 at 6:17
  • 1
    @templatetypedef: Since dynamic_cast looks and feels like a template function, perhaps you could introduce a wrapper, use #define dynamic_cast checked_dynamic_cast to force use of the wrapper, and then somehow get a list of all instantiated versions of the template? Getting the inheritance graph is fairly easy, there are a number of tools that expose it including for example doxygen's XML output. Combining that information and applying some heuristics should give you the warning you seek.
    – Ben Voigt
    Mar 16, 2011 at 6:20
0

That's the whole meaning of the dynamic cast: It is dynamic i.e. it is checked at the runtime not at the compilation time.

The compiler only checks if the casted classes are polymorphic. If the classes are not polymorphic then there will not be enough information to check the casting at the runtime.

And finally after the dynamic cast the program should check if the received pointer is not null. If you cast to a reference then you should catch std::bad_cast.

-3

Truth is when you use dynamic_cast you can cast any polymorphic type pointer to any polymorphic type pointer even when classes are unrelated to each other.

If you want compile-time check you need to use other casts - static_cast or implicit conversions - that might not suit for some other reasons in many cases.

The solution we use when dynamic_cast is needed is a templated function that does dynamic_cast and throws an exception if a null pointer is obtained. That's of course a runtime check only, but it's rather reliable.

1
  • 1
    -1: You can dynamic_cast a polymorphic class to another polymorphic class only if their RTTI are related. Otherwise You get a 0 ptr (if You cast to ptr), or You get a bad_cast exception if You cast to the ref to the non related polymorphic class.
    – TrueY
    Apr 29, 2014 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.