简体   繁体   中英

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?

GCC有选项-Wold-style-cast ,如果发现任何C -Wold-style-cast ,它会发出警告。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM