[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: dynamic_cast performance overhead
From:       Michael Buesch <mbuesch () freenet ! de>
Date:       2005-06-03 12:33:42
Message-ID: 200506031433.42682.mbuesch () freenet ! de
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


Quoting David Faure <faure@kde.org>:
> On Thursday 02 June 2005 22:26, Michael Buesch wrote:
> > Quoting Michael Buesch <mbuesch@freenet.de>:
> > > Maybe a template function or something similiar would be better.
> > 
> > Such as this. This is tested and prints a friendly debug
> > message in DEBUG mode. Otherwise is optimized by the
> > compiler to nothing (normal reinterpret_cast).
> > I checked the asm output and it looks quite nice to me.
> > I consider to use this (or something similiar) in PwManager.
> > Detects programming errors in DEBUG mode and is fast otherwise.
> But it relies on the developer testing all code paths in the program.
> 
> I think this is a bit dangerous. If someone isn't sure whether a dynamic_cast
> is needed, and uses this cast "in order to check that", he might conclude the 
> wrong thing: if he doesn't hit the "cast failed" error, he might think that the
> cast is always ok, and leave things as is, i.e. for the end user an unchecked cast.
> And then the end user does something a bit differently - and gets a crash.
> 
> IMHO it's better to simply know, as a developer, whether a cast needs to
> be checked or not; instead of relying on empirical experience (some testing)
> to tell you.

Exactly, you _must_ know if you have to use dynamic_cast or not.
This was _only_ a helper function to use if the developer thinks:
"I don't need dynamic_cast here", and he was actually wrong with
his assumption.
It is to halt/crash the program in a well defined way (in the
cast helper function) and not at some random place later. (Or even
it may not crash at all later and work by accident. Nice hidden bug).

fast_dyn_cast is to be used when:
* A static_cast would always do, too. Its only purpose is to
  do sanity checks, to check if the developer was right.

and dynamic_cast is to be used when:
* We can not static_cast.

> > 	to = reinterpret_cast<ToType>(from);
> Should be simply static_cast.

Ok, my fault.

-- 
Greetings, Michael



[Attachment #5 (application/pgp-signature)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic