views:

459

answers:

4

Hello

How can I locate all old C-style cast in my source?

I'm using Visual Studio, may be there is some compilator warning that I have to enable? Or use some software tool for this?

+10  A: 

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

Andrew Keeton
But I'm using Visual C++ (MFC) :(
ju
You might want to check the Visual C++ compiler switches to see if there is one to turn off this warning or if they have one which you can specify disabling certain warnings. If there is not such a switch, you could disable the warning with a pragma in the files which are causing the offense. I have used with with Visual C++ to disable some warning about the length of STL names when compiling.
Glenn
+4  A: 

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.

Reed Copsey
+3  A: 

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

Timo Geusch
A: 

Why do you need that? Do you want to get rid of all of them?

C-style casts for fundamental types like (double) are safe and looks much better than static_cast<double>(). I personally prefer this style.

In case of polymorphic classes, the problem of C-style casts is grossly exaggerated imho. It may be a problem only if you use multiple inheritance and have to do cross casting. By default, C-style casts won't work there. But in most cases () works exactly like static_cast<>() and I wouldn't waste time on replacing them. Anyway, for polymorphic types I use C++-style casts when write new code or change something.

UPDATE:

Guys, it seems I expressed my opinion not so clearly. Yes, I know the theory: C++-cast is good, C-cast is evil. This rule (and most others) is needed to save newbies from C++. But it doesn't mean that you always need to follow it.

My point is that C-style casts are not so terrible. If you write some function like

int convertFooToBar(double i_foo)

, there are no reasons to use C++-style casts in implementation. They are not safer than (int) but look messy. Casting is not tricky in such case. It is nothing bad or tricky in casting at all: sometimes casts are natural.

About searching: I don't remember that I was searching for any cast statement in last 10 years, though I'm maintaining several million lines of legacy C++ code on daily basis. I don't even remember any serious problem due to wrong casting.

Original question is about replacing already written C-style casts. So, my answer is:

Yes, C++-style casts are better and safer than C-style ones. But it doesn't mean that you need to search for old ones in your code and try to get rid of all of them. Actually, it is not a real problem. Just don't waste your time. You may spend it on searching and deleting some out-of-date code. It would be more useful for your code base.

bocco
While it might work like a static_cast, the problem is that it might silently switch to working like a reinterpret_cast when you change some surrounding code. The problem is that you don't properly express what you want, and the compiler is free to perform whichever cast is the best fit. Change some surrounding code, and your C-cast changes its meaning and breaks your code.And of course, a bonus problem is the subject of this question: They are impossible to search for. It's a bit awkward not being able to locate the casts you use, especially when they're so error-prone.
jalf
and finally, it is *intentional* that they're ugly. Casts *are* an ugly trick. They should look ugly to make it clear that they're happening, and to discourage you from overusing them. ;)
jalf
This very problem, where the user can't find the casts in his code, is one reason to favor the C++ style casts.
JohnMcG