Programmer Religious War – C vs C++ Style Casts.
There are many issues in the programming world that seem to divide people down religious lines. These issues seem to cause endless internet flame wars and programmer rants without ever coming to a consensus.
photo credit: Philippe Put
One of these issues I’ve been seeing a lot lately is about whether to use the old school C style casting, or use the specific C++ style casts.
Here at Riot C++ style casts are part of our coding standard and the use of C style casts is generally discouraged in our codebase. This is a pretty common standard, as seen even in the Google Style Guide. But, conversely you have several major game studios with the inverse standard, who ban any use of C++ style casts and allow only C style casts. I’ve spoken with excellent programmers at places like Blizzard and Insomniac who say they will personally never use C++ style casting in their game code.
Isn’t it quite strange that multiple AAA studios can have completely opposite coding standards on this topic? At first I thought, like with a lot of C++ features, this was a console studio vs PC studio type of issue, but from my conversations with colleagues, I dont even think this is the case anymore. It seems to simply be a matter of style and personal taste.
C++ Style Casting
syntax: int myInt = static_cast<int>(myFloat);
For a detailed description of C++ style casting check out this paper on the subject. C++ broke up casting into several different keywords with different use cases, here I will take you through a super-quick overview of them:
static_cast: The most common C++ cast. Casts between like types, with some compile-time checking.
dynamic_cast: Casting with runtime type checking. Checks to make sure a cast between a parent and a child class is valid, and can return NULL or throw an exception if it is not.
const_cast: Used for adding or removing the const keyword from a type.
reinterpret_cast: Used to cast between any two types, even between incompatible or opaque types.
There are many advantages to using C++ style casts over C style, including:
1) The intent of the programmer is more explicit. Since there are different keywords for different types of casts, it is easies to understand what the programmer wants to accomplish. For example, when you see a reinterpret_cast, you know the programmer is intentionally casting between incompatible types for a specific reason.
2) C++ style casts are safer. The most common cast that replaces C style casts is the static_cast. If you try to static_cast between incompatible types, the compiler will warn you, whereas it will not with a C style cast. For example, if you have the forward declaration of a class, but forgot to include the complete definition, and perform a cast to that type, then a C++ static_cast will complain at compile time, however a C style cast will compile fine and most likely crash at runtime.
3) C++ style casts are greppable. It is easy to do a find in files to find all instances of a C++ style cast such as const_cast in your codebase. However, this is much more difficult with C style casts.
4) dynamic_cast provides runtime type checking, while a C style cast can not. Most games have RTTI disabled for performance reasons, so this one isnt as interesting, but this feature is still occasionally useful in tools code.
C Style Casting
syntax: int myInt = (float)myFloat;
C Style casting is the one that most people grew up with and are most familiar with. Put some parenthesis around the type you want to cast to and away you go. So why do so many people stick with this old school way when C++ has eliminated the need for it?
1) The C style syntax is much easier to read. The C++ style casts takes the short form of the C style, then adds a verbose keyword, and angled brackets, making it much uglier and harder to quickly parse. It is very important for programmer’s to be able to read code quickly, and easily get an understanding for what is happening at a glance. Many people feel that the janky syntax of C++ casts obfuscate the code when compared to the more elegant C style casting syntax.
2) Everybody is already familiar with C style casting. It is widely used and understood, and when writing code using them you can almost guarantee that any other C/C++ programmer recognizes it and understands that. C++ style casting is just not as widely used, making it so that your code is not as universally understood by other programmers.
3) Since the C++ casts are more complex than normal ones there is more potential for programmer’s to not fully understand them and potentially misuse them. Simply using static_cast for example can sometimes change the value of a pointer, and as one programmer I know expressed: “I never use any language construct that requires moving the ‘this’ pointer behind my back.”
4) dynamic_cast will slow down your game because it uses RTTI. In addition to being an expensive operation, relying on dynamic_cast means that your code is probably badly designed and very brittle.
My Stance
At Riot we are particularly partial to the use of C++ style casts, and I fall squarely on that side of the fence as well, however I agree completely that dynamic_casts are bad news in runtime code. Like many studios we have RTTI disabled, so the option to use them is not even there.
This is, however, an unlikely place for me to fall on this issue. I typically am a “readability above all” type of programmer, and the arguments that C++ style casts make code uglier and harder to read at a glance ring true with me. However, I feel that the added safety of C++ style casting and the explicit intent they communicate much outweigh this downside and make them a welcome addition to any C++ codebase I am working in.
How do you handle casting in your codebase?
Facebook Comments
2 Comments to “Programmer Religious War – C vs C++ Style Casts.”
Leave a Reply



I prefer C++ style casts any time I’m asking pointers to objects. If I’m just casting a float to an int, I’ll usually use C style for brevity.
C++ casts let you specify what you are doing with greater precision, which is something programmers should always strive for IMO. I can see the argument against supporting RTTI on a console game, but I don’t understand the readability one. If you use and/or read C++ casts a lot your brain will just start parsing it like all the other cryptic nonsense that makes up programming syntax.
Nice comparison post. One note in the C style casting example; I believe the cast types should be changed. So:
int myInt = (float)myFloat;
should be
int myInt = (int)myFloat;