Re: Turn off checks in dynamic_cast of reference types in release builds

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Erik <esigra@xxxxxxxxx> writes:

> Ian Lance Taylor skrev:
>> Erik <esigra@xxxxxxxxx> 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.


#ifdef DEBUG_MODE
template<typename T1, typename T2>
T1& dynamic_reference_cast(T2& r) { return dynamic_cast<T1&>(r); }
#else
template<typename T1, typename T2>
T1& dynamic_reference_cast(T2& r) { return static_cast<T1&>(r); }
#endif

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux