1

I would like to know this in case RTTI is disabled in some compiler environment.

Use case:

I used this in my code and it worked on my machine but during integration testing the code failed to run properly, rather crashed.

I supposed it would have returned null in case RTTI is not there which was handled in my code but the behavior was unexpected.

What should I have checked to terminate it elegantly?

Also, when I used a wrapper of dynamic_cast (defined in our inhouse framework), it worked fine. What kind of implementation would that wrapper have to fulfill this requirement?

Vicky
  • 105
  • 1
  • 2
  • 10
  • 1
    [This gist shows how to detect whether RTTI is enabled](https://gist.github.com/brimston3/2be168bb423c83b0f469c0be56e66d31). – Raymond Chen Oct 24 '19 at 18:23
  • maybe you should post code and compiler info. Are you `dynamic_cast`ing a pointer or a reference? – Brad Allred Oct 24 '19 at 18:28
  • Using `virtual` functions has been tried and rejected? – user4581301 Oct 24 '19 at 19:39
  • 1
    what behaviour do you want the `dynamic_cast` to have if RTTI is disabled? – M.M Oct 24 '19 at 20:34
  • @M.M: I want something with similar functionality but works without RTTI. What I found is, we have some internal proprietary framework which does exactly this. I guess there is no such alternative though the solutions provided here are quite elegant. Thanks. – Vicky Nov 23 '19 at 11:52

1 Answers1

0

Raymond Chen left a fabulous link in a comment about how to detect if RTTI is enabled.

To complete on that, you could override dynamic_cast with:

#if !defined(RTTI_ENABLED)
    #define dynamic_cast _NullPtrTFn
#endif

template <typename T1, typename T2>
T1* _NullPtrTFn(T2* p)
{
    static_cast<T1*>(p); // try fail to compile (https://godbolt.org/z/hcjKki)
    return static_cast<T1*>(nullptr);
}
tuket
  • 3,232
  • 1
  • 26
  • 41
  • Is this different to `#define dynamic_cast static_cast` ? – M.M Oct 24 '19 at 20:35
  • @M.M Yes, because 1) static_cast would throw a compile error if you try to down-cast 2) In case of up-casting it wouldn't return nullptr – tuket Oct 24 '19 at 20:53
  • "static_cast would throw a compile error if you try to down-cast" - not true – M.M Oct 24 '19 at 21:35
  • How does this even compile? The real `dynamic_cast` only has 1 template parameter, but `_NullPtrTFn` has 2, so shouldn't any use of `dynamic_cast` fail to compile when `RTTI_ENABLED` is undefined? I would think something like `template T* _NullPtrTFn(void*) { return nullptr; }` would be a better implementation, yes? – Remy Lebeau Oct 24 '19 at 22:16
  • @RemyLebeau It does compile because T2 can be omitted (is a parameter of the function). I think what you propose works just as well and is simpler. The best solution would be the one that fails to compile when dynamic_cast<> would fail to compile. I will edit my answer when I have some time – tuket Oct 25 '19 at 07:36
  • 1
    @tuket OK, I guess I didn't take [Template Argument Deduction](https://en.cppreference.com/w/cpp/language/template_argument_deduction) into account. Thanks. – Remy Lebeau Oct 25 '19 at 17:33