This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Turning off C++ casting??
- From: Mongryong <Mongryong at sympatico dot ca>
- To: Joe Buck <jbuck at synopsys dot com>
- Cc: gcc at gnu dot org
- Date: 19 Feb 2003 21:05:12 -0500
- Subject: Re: Turning off C++ casting??
- References: <B11C8538-4464-11D7-B22E-003065A77310@apple.com><1045699303.1364.76.camel@refugeeMedia> <20030219174232.A21518@synopsys.com>
On Wed, 2003-02-19 at 20:42, Joe Buck wrote:
> On Wed, Feb 19, 2003 at 07:01:43PM -0500, Mongryong wrote:
> > What I want to know is whether GCC allows you to optimize
> > 'dynamic_cast'ing: gcc -fno_dynamic_cast .... I don't think gcc has
> > such a switch.
>
> It doesn't. Also, optimizations must preserve semantics, as in returning
> null if the object isn't of the right type, adjusting the pointer value
> in the case of multiple inheritance, etc.
I guess what I would like is the notion of a 'assert cast' - just like
assert is the DEBUG version of 'throwing an exception'.
#ifdef NDEBUG
template<typename RET, typename RES>
inline RET& assert_cast(SRC& arg) {
return *((RET*)((void*)&arg));
}
#else
template<typename RET, typename RES>
inline RET& assert_cast(SRC& arg) {
return dynamic_cast<RET&>(arg);
}
#endif
ExtendedObject& obj = assert_cast<ExtendedObject, BaseObject>(baseObj);
Does anyone see any problems with this implementation (from a GCC point
of view)?