This diagnostic warning has been added at the request of users.
The analyzer allows you to detect explicit type conversions written in the old C language style in a C++ program. It is safer in the C++ language to convert types using operators static_cast, const_cast and reinterpret_cast.
The V2005 diagnostic rule helps to perform code refactoring and replace the old type conversion style with a new one. Sometimes it helps to detect errors.
Here are examples of constructs that will trigger this diagnostic message:
int i; double d; size_t s; void *p; ... i = int(p); //V2005 d = (double)d; //V2005 s = (size_t)(i); //V2005
The V2005 diagnostic message is not generated in three cases.
1. This is a C program.
2. The conversion target type is void. This type conversion is safe and is used to emphasize that there is a result which is not used anyhow. For example:
3. The type conversion is located inside a macro. If the analyzer generated the warning for macros, there would be a lot of reports when different system constants and macros are used. And you cannot fix them anyway. Here you are some examples:
#define FAILED(hr) ((HRESULT)(hr) < 0) #define SRCCOPY (DWORD)0x00CC0020 #define RGB(r,g,b)\ ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))\ |(((DWORD)(BYTE)(b))<<16)))
Special settings of the V2005 diagnostic
On an additional request of our users, we have implemented the feature allowing you to manage the V2005 diagnostic's behavior. Now you can use compiler switches (Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definition, locally for a file or globally) to define the working mode of this diagnostic through one of the values: PVSSTUDIO_2005EX_ALL or PVSSTUDIO_2005EX_NO_SIMPLE_CAST.
Three modes are available:
a) Default mode: each type conversion in the C style triggers a warning prompting you to use such constructs as static_cast, const_cast and reinterpret_cast instead of a type conversion.
b) PVSSTUDIO_2005EX_ALL: each type conversion in the C style causes the analyzer to display a recommendation about what keyword(s) should be used instead. In rare cases, generating single wrong recommendations is possible due to conversion of complex template types. Another rare situation is also possible that the analyzer fails to detect the conversion type and displays an ordinary warning without specifying the exact conversion type.
c) PVSSTUDIO_2005EX_NO_SIMPLE_CAST: this mode is similar to the previous one, but in this case warning is generated only when at least one type in conversion is pointer or when conversion type predicted is more complex than static_cast.