How can I locate all old C-style cast in my source?
I'm using Visual Studio, may be there is some compiler warning that I have to enable?
C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type). (<type>)<value> This example casts an int to a double for the purpose of avoiding truncation due to integer division: double result = (double)4/5; Popular pages.
Static casts are only available in C++. Static casts can be used to convert one type into another, but should not be used for to cast away const-ness or to cast between non-pointer and pointer types.
GCC has the option -Wold-style-cast
which will warn you if it finds any C-style casts.
I don't know of a compiler switch that reports these, or anything else built-in in Visual Studio.
However, here is a Perl script that will search your source tree and find all of the C style casts in your source. It works fairly well for tracking them down.
Unfortunately there isn't a compiler warning in Visual C++ that points out these casts (at least not one that I know of), although it looks like PC-Lint does provide a warning/note that warns you if you use an old-style cast. Depends if you're willing to spend the money on PC-Lint - in my opinion it's definitely worth for all the issues you can find with it...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With