Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find (and replace) all old C-style data type casts in my C++ source code?

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?

like image 277
ju. Avatar asked Aug 13 '09 15:08

ju.


People also ask

What is C style cast?

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.

Does C have Static_cast?

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.


3 Answers

GCC has the option -Wold-style-cast which will warn you if it finds any C-style casts.

like image 112
Andrew Keeton Avatar answered Oct 01 '22 14:10

Andrew Keeton


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.

like image 22
Reed Copsey Avatar answered Oct 01 '22 13:10

Reed Copsey


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...

like image 42
Timo Geusch Avatar answered Oct 01 '22 15:10

Timo Geusch