This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Turn off checks in dynamic_cast of reference types in release builds
- From: Ian Lance Taylor <iant at google dot com>
- To: Erik <esigra at gmail dot com>
- Cc: MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Wed, 30 Sep 2009 00:13:05 -0700
- Subject: Re: Turn off checks in dynamic_cast of reference types in release builds
- References: <4AC3007F.4040609@gmail.com>
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 ?
I don't think there is any other way. While dynamic_cast to a
reference type is "like" an assertion, it is not in fact an assertion.
The compiler does not provide a way to disable it.
> 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.
Ian