C++. Search for explicit C-style type casts: a = (int)b; [closed]

Closed. This question is off-topic. Answers to it are not accepted at the moment.

Want to improve this question? Update the question so that it it fit into the theme of Stack Overflow in Russian.

Closed 5 years ago.

Improve the question

A question about the benefits of diagnostics. One person suggested implementing the PVS-Studio analyzer to search for all explicit type casts in the C style. That is, to identify constructs type:

int *x = (int *)y; float a = float(b); float c = (float)(d);

The goal is to replace all these casts with more secure versions-reinterpret_cast/static_cast/const_cast. In the process of such refactoring, some defects may well be identified.

Of course, this is not the identification of real errors. And if this diagnostic is implemented, it will be located in the [Customer's Specific Requests] section and disabled by default.

However, even in the benefits of this option, I am not sure. I decided to ask a question. Need anyone else looking for all the explicit C-style type casts? Would someone want to do a similar refactoring of their code?

Author: AndreyKarpov, 2011-09-28

1 answers

There are different options for such a cast. 1) meaningless

float d = 3;
float c = (float)(d);

For these, you should offer to simply remove it.

2) at the request of the api. This is when some external function is declared with one type, and in the code we have a different type and do a forced cast, although we could immediately declare it correctly.

3) conversions that are not needed are when the resulting expression is guaranteed to fit into the result. (for example, char in int).

But the main thing is that after any advice The PVS-Studio code was compiled at the maximum level of the compiler's finesse.

 1
Author: KoVadim, 2011-09-28 06:50:00