Josef “Jeff” Sipek

Awful, Awful C++: Casting

C++ has several ways of casting. Sadly enough, the names are mostly unintuitive. I asked several people I know (some of which happen to be, or have been instructors for the introductory C++ class), and I managed to construct the following list. Not all of them knew what all the casts do. This is a very C++ symptom; most people don’t know significant fractions of the language. This is mostly because C++ is a very complex language and no one teaches/tells you about everything.

Of course, Stroustrup’s The C++ Programming Language describes all the cast constructs.

converts between related types. The types have to be convertible. For example, ints can be trivially converted into enums. Floating point variables can be converted to integral types — truncate the decimal portion.
converts between unrelated types. For example, from an integer to a pointer, or between two unrelated pointer types (e.g., float* and int*).
is used when you want to remove const or volatile qualifiers.
attempts a type conversion but performs run-time checks to see if the conversion is valid. This is useful for converting pointer types — from a base class type to a subclass type. If the conversion is not valid, dynamic_cast returns a null pointer.
C-style cast
is the confusing cast in C++. It is a combination of static_cast, reinterpret_cast, and const_cast. That is, it can be used to add or remove const-ness, volatile-ness, convert between related or unrelated types (between ints and pointers, different type pointers, and between the various integral types and floats).

All in all, it really isn’t that bad once you see a list.

I started this post with the intent to show how ridiculous C++ casting is, but I think that all I can really say is just that I don’t agree with some of the ways it is done. I don’t like how verbose the cast syntax is. (I am aware that unnecessary casting is bad; in C, I get frustrated whenever I see people casting malloc return value instead of simply assigning it.) On the other hand, I have to agree with people that say that it’s easier to grep for C++-style casts.

The issue with C++ here is that it allows C-style casts to coexist with C-style casts. I’ve been told by C++ fans that one cannot treat C++ as if it were C. One way that would accomplish that would be making C++ look different than C. An easy thing would be to get rid of C-style casts. (I am aware that this will never happen.) Not only do they confuse C programmers trying to use C++, and C++ programmers that haven’t been told about all the features of the language, they also make the compiler’s job more difficult and the software potentially buggier. In C++, the C-style cast is the nuke. It tells the compiler to just do the conversion. C++ gives the programmer 4 different casts, using the right one (or ones if more than one is required) at the right time means the compiler can do more optimizations and more sanity checks making the code faster and less likely to be buggy.

So, C++ isn’t awful because it has a bunch of different casts, but because it allows them co-exist with the C-style cast.

Powered by blahgd