« Vishy's Indian English Dictionary: funda | Main | Vishy's Useless Factoid of the Day #10: What does serendipity have to do with Sri Lanka? »

May 30, 2005

The cast system in C++

A long time ago, when the prevalent language was C, all casts were equal in the eyes of society. Then Stroustrup created C++. As C++ grew, its cast system became explicit and rigid. The cast system in C++ has four casts -- dynamic_cast, static_cast, const_cast and reinterpret_cast. The function of these casts is to convert from type to another. The types involved in a cast are usually pointer types, though this is by no means necessary. Even though the intent of the cast system in C++ was merely to mark the different kinds of type conversion, programmers have accorded the four casts different levels of status and desirability. We will learn about these casts in order of their desirability and the destabilizing effects they have on programs.

The highest cast is the dynamic_cast. When converting a pointer of one type to a pointer of another type, dynamic_cast performs a runtime check to make sure that the cast is safe. If the cast is unsafe, a dynamic_cast results in a null pointer. Other programming languages, such as C# with its as operator, have emulated this cast, evidently as a form of Sanskritization. By performing a runtime check, the dynamic_cast distinguished itself as the safest of all the casts. It is least likely to cause access violations or other forms of unrest in a program and therefore leads to the least violent death for a program.

The next most prestigious cast we will consider is the static_cast. This cast does not believe in incurring performance penalties by performing runtime checks. When static_cast-ing from a Fruit* to an Orange*, the conversion will succeed, but if the pointer was really pointing to an Apple, then invoking the getNumSegments() method on it would cause an access violation. The static_cast is less reflective and more worldly because it takes the type information provided in the static_cast statement as the literal truth, even if it results in a little violence down the road.

The runtime information available to the dynamic_cast combined with the authoritarianism of the static_cast has given the nexus of these two casts enormous influence over the other casts. A variable may be created as a Fruit*, but it has the opportunity to be reborn legitimately through the rituals of the upper casts as a Banana*. This phenomenon of being born again has led to this nexus being called the twice-born casts.

The next cast we will examine is the const_cast. The function of this cast is to cast away the const-ness of a pointer. Const-correctness, when used correctly, is a very important and useful feature in C++. In the illusory universe of the stack, where variables and pointers are constantly in flux, it imbues them with an unchanging beauty for as long as they are in scope. The const_cast is willing to trade away this const-ness in return for the convenience of changing a const variable's value. Unless blessed by the mutable keyword, casting away the const-ness of a value is a serious risk to the correctness of a program, which may result in either violent death or unpredictable behavior.

The cast at the very bottom of the cast system is the reinterpret_cast. Faint-of-heart programmers shrink away in horror from the mere shadow of the reinterpret_cast. The reinterpret_cast does the dirty work of converting between unrelated types. Through the menial hands of the reinterpret_cast, a Fish* may be reinterpreted as a Bicycle*. Most coding guidelines in C++ either prohibit the reinterpret_cast or impose severe restrictions on its life. It should be clear why this is the case. If an object is arbitrarily transformed into another of an unrelated type at random, the natural order inherent in society would be transformed to chaos.

Coming across each of these casts designated explicitly in C++ code is an eyesore to code readers and is a testament to the ugliness of the explicit cast system in C++. It is important to realize that each of these casts has its place in an advanced systems programming language like C++. Any type conversion is necessarily an unnatural act to some degree. Explicitly marking them as such is merely a constant reminder to writers and readers of code alike that objects of all different kinds must live together in harmony.


(If the extended metaphor in the above explanation seems entirely lost on you, you are probably not familiar enough with Indian culture. Feel free to email me or talk to me about it. I hope you will still find the technical content useful, because it is accurate. If you are familiar with Indian culture and know what I am referring to, you should know that I don't believe in it one bit. This is intended as merely an extended inside joke that occurred to me in a moment of inspiration.)

Posted by Vishy at May 30, 2005 12:19 PM

Comments

Clever, clever. As usual :)

Posted by: Stephen at May 31, 2005 01:48 AM