This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Turning off C++ casting??


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)?




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]