[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-02 20:26:43
Message-ID: 200506022226.44076.mbuesch () freenet ! de
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


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.


template<class ToType, class FromType> inline
ToType fast_dyn_cast(FromType &from)
{
	ToType to;
#ifdef DEBUG
	to = dynamic_cast<ToType>(from);
	if (!to) {
		// here should be some debugging stuff.
		cout << "Cast failed!" << endl;
		exit(1);
	}
#else
	to = reinterpret_cast<ToType>(from);
#endif
	return to;
}


Usage:

BaseClass *b;
DerivedClass d;
b = &d;
DerivedClass *p = fast_dyn_cast<DerivedClass *>(b);


Finally just tweak VIM to color fast_dyn_cast like dynamic_cast. ;)

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