V2005. C-style explicit type casting is utilized. Consider using: static_cast/const_cast/reinterpret_cast.
This diagnostic rule was added at users' request.
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:
(void)fclose(f);
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)))
The V2005 diagnostic's levels of certainty
By default, all warnings issued by the V2005 diagnostic rule have the second (Medium) level of certainty. If a suspicious code fragment is in a template and there's a casting to a type template parameter, then the level of certainty downgrades to the third (Low).
Look at the synthetic example:
template <typename TemplateParam>
void foo(const std::vector<SomeType>& vec)
{
auto a = (TemplateParam)(vec[0]); //+V2005 //3rd level
auto b = TemplateParam(vec[3]); //+V2005 //3rd level
// ....
auto i = (int)a; //+V2005 //2 level
auto i = int(a); //+V2005 //2 level
// ....
}
Without instantiating the template it's hard to understand what's hiding behind 'TemplateParam'. If there's casting to the known type within a template, then the analyzer will still issue the Medium-level warnings. If the Low-level warnings are pointless to you, you can suppress them with a following comment:
//-V::2005:3
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. In the general header file or pvsconfig-file you should write a special comment. Here is an example of usage:
//+V2005 ALL
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) 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.
//+V2005 ALL
c) 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.
//+V2005 NO_SIMPLE_CAST
References:
- Terminology. Explicit type conversion.
- Wikipedia. Type conversion.