Turn off checks in dynamic_cast of reference types in release builds

Brian Budge brian.budge@gmail.com
Wed Sep 30 14:23:00 GMT 2009


How about:

#ifdef NDEBUG
#define dynamic_cast_ref static_cast
#else
#define dynamic_cast_ref dynamic_cast
#endif//NDEBUG

Would that suit your needs?

  Brian

On Wed, Sep 30, 2009 at 12:31 AM, Erik <esigra@gmail.com> wrote:
> Ian Lance Taylor skrev:
>> Erik <esigra@gmail.com> writes:
>>
>>
>>> Bjarne Stroustrup wrote in his book The C++ programming language that;
>>> while a dynamic_cast to a pointer type is a question (the result should
>>> be checked), a dynamic_cast to a reference type is like an assertion. We
>>> have followed this advice in our project. Now we need to disable these
>>> assertions in release builds, so that dynamic_cast<Derived &>(base) is
>>> just as efficient as static_cast<Derived &>(base). But I do not know how
>>> to tell this to the compiler. I only found some other checks in gcc,
>>> that can be disabled for release builds, such as the Ada checks (-gnatp).
>>>
>>
>> -Ddynamic_cast=static_cast ?
>>
> That would of course not work, because it would affect all
> dynamic_casts, not just those to references.
>
>
>>> Or is the feature that I need missing in gcc? Maybe I have to use one of
>>> the many hacks called down_cast, safe_cast and so on, that can be found
>>> on the web? Like this one:
>>> http://smolsky.net/index.php/2009/09/14/down_cast-v2
>>> If so, which of those hacks is the correct one that should be used?
>>>
>>
>> Since you don't care about dynamic type safety you can just use
>> static_cast.
>>
>
> That would certainly not work, because then the assertions in the form
> of dynamic_casts to reference types would not be checked in debug builds.
>



More information about the Gcc-help mailing list