One of the few truly compelling reasons to use C++ over C is the casting syntax. Earlier today, Stephan Wolf (another MVP) posted the following on a newsgroup:
Well, the following should also work without a warning: ULONG x = (ULONG) strlen(...) ..because an explicit type cast tells the compiler "yes, I do this conversion intentionally and I am aware of any possible truncation".
This was in response to someone complaining that strlen’s return type size changed. The real truth, of course, is that the original code was using the *wrong* return type, but that’s another story for another day.
The problem with C-style casts, as was pointed out by Bjarne Stroustrup ages ago, is that they are virtually impossible to detect with a regular expression. Casts often hide bugs, often intentionally – that code above has a bug in it, unless you happen to know something about the input to strlen(). So, you’re left with a bit of syntax that often hides bugs and can’t be filtered through a checkin script, for instance.
C++, on the other hand, provides several casting operators that are all much easier to detect. Stroustrup said that he thought casting operators should be ugly because casting itself is an ugly operation. To wit:
const_cast() static_cast() reinterpret_cast() dynamic_cast() [caveats]
These operators are easier to grep for, and better yet, are separated by purpose, so you don’t have to worry about false-positives for intentional downcasts to derived casts and such.
[...] When I took Stephan Wolf’s example of fixing a warning with a cast the other day as an example of a place I’d have preferred a C++ casting operator, I didn’t mean to get the Mother Of All Holy Wars started again. So, I’m going to be slightly rude and check out of that discussion once and for all. There’s already enough C++ vs. C in the Internet’s archives for one millennium. [...]
[...] That second parameter is a pain; size_t is 64 bits wide on x64, so it if you build with /Wp64, you need a cast. And you know how I feel about C casts. [...]
[...] 9 out of 10 dentists (and at least one blogger) agree that it is better to limit the use of casting in your code. If you’re going to do that, you’ll find out early on that you have to match the types your program uses to the types that your APIs use. Otherwise you will create porting difficulties and compiler warnings, which usually lead right back to casts. [...]
[...] The reason that they’re silent is also the second great reason to use CONTAINING_RECORD: casts. I’ve written before about how much I like casts in code, so I’m implicitly for anything that eliminates casts. Since CONTAINING_RECORD returns the right type by definition, you no longer have to cast it. So: [...]
Search of implicit type conversion or explicit type conversion in C style is a headache. If you develop the x64 program with use Visual Studio 2005 – you has carried. You can take advantage of utility Viva64. If is not present, try to look for another lint-like similar utility.
http://www.viva64.com/download.php
And here many the interesting references, devoted migration on 64-bit systems:
http://www.viva64.com/links.php