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

后端 未结 4 1436
一生所求
一生所求 2020-12-10 10:55

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?

相关标签:
4条回答
  • 2020-12-10 11:04

    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.

    0 讨论(0)
  • 2020-12-10 11:07

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

    0 讨论(0)
  • 2020-12-10 11:10

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

    0 讨论(0)
  • 2020-12-10 11:19

    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.

    0 讨论(0)
提交回复
热议问题