Bug Vanquisher

19 April 2008

The Myth Called Overloading

Filed under: Dev inside! — Tanveer Badar @ 11:23 PM

There are some serious word press screw ups in this post. No smileys, ever! Yet word press insists. Also, line breaks are disappearing like mad.

There is no such thing as function overloading in this world! Period.

Whatever you see in HLLs is the compiler lulling you into the false sense that function overloading exists.

Cases in point:

1- C++: Most major compilers (at least from Microsoft) use name mangling. Here is dump of four constructors on std::bad_cast from msvcrt.dll

Mangled mode

Pretty mode

??0bad_cast@@AAE@PBQBD@Z bad_cast::bad_cast(char const * const *)
??0bad_cast@@QAE@ABQBD@Z bad_cast::bad_cast(char const * const &)
??0bad_cast@@QAE@ABV0@@Z bad_cast::bad_cast(class bad_cast const &)
??0bad_cast@@QAE@PBD@Z bad_cast::bad_cast(char const *)

2- CLR:Everything compiles down to IL, whether you like it or not. And here’s IL for typical function calls:

call class System.ServiceModel.Channels.Message System.ServiceModel.Channels.Message::CreateMessage(
class [System.Runtime.Serialization]System.Xml.XmlDictionaryReader,
int32, class System.ServiceModel.Channels.MessageVersion)

from System.ServiceModel.Channels.Message.

That’s it, you get the whole nine yards. Return type + fully qualified function name + fully qualified parameter types. It does not matter whether you use call, callvirt or calli. If its a call, its going to use everything fully qualified.

3- You may want to argue about operator overloading. But that’s just syntactic sugar.  Here is >> from std::basic_istream.

Mangled mode

Pretty mode

class std::basic_istream
<wchar_t,struct std::char_traits<wchar_t> > &
std::operator>>
<float,wchar_t,struct std::char_traits<wchar_t> >
(class std::basic_istream
<wchar_t,struct std::char_traits<wchar_t> > &
,class std::complex<float> &)
??$?5M_WU?$char_traits@_W@
std@@@std@@YAAEAV?$basic_istream
@_WU?$char_traits@_W@std@@@0@
AEAV10@AEAV?$complex@M@0@@Z

4- In CLR, the case is even simpler. You get op_Add straight away, then, its off to fully qualified function calls in IL. End of story.

It does not exist. Anywhere. Ever.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: