But besides that, /Wp64 switch has one more drawback that confuses the programmers who are not familiar with it. It concerns the issue of developing a code containing some templates. Here is an example.
In the vast ocean of the Internet you may find the following example in the comments to Visual C++ developers' blog:
vector<size_t> vs; // create the element vector size_t vector<unsigned int> vi; // create the element vector unsigned int size_t s; // there is a variable of size_t unsigned int i; // there is a variable of unsigned int vs[0] = s; // there must be no warning vs[0] = i; // there must be no warning vi[0] = s; // there must be warning (*0) vi[0] = i; // there must be no warning s = vs[0]; // there must be no warning i = vs[0]; // there must be warning (*1) s = vi[0]; // there must be no warning i = vi[0]; // there must be no warning (*2)Consider that the types size_t and unsigned int must coincide in 32-bit mode.
Now we compile this code in 32-bit mode in Visual C++ 2005 and get the following warnings. In the line marked with (*1) everything is alright:
warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data
But in the line marked with (*2) we also see the same warning:
warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data
Yet there should not be any warning here.
And in the line (*0) there is a missing warning.
But if you compile the code in 64-bit mode, you get the warning on the lines marked with (*0) and (*1) as it should be:
warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data
The author of the example, Stephan T. Lavavej, discusses the problems of implementing /Wp64 switch in templates. The point is that the compiler switch /Wp64 is implemented through the special key word __w64, added to the type description:
#ifdef _WIN64 typedef __int64 MySSizet; #else typedef int __w64 MySSizet; // Add __w64 keyword #endifBut this key word does not introduce a new data type and that is why the template classes vs and vi in this code are identical:
typedef __w64 unsigned int size_t; vector<__w64 unsigned int> vs; vector<unsigned int> vi;And although vs and vi seem to have different types, the compiler considers them identical not without reason and generates false diagnostic warnings.
What to do? In Microsoft Connect there is an error, but, as they have written, they are not going to fix it. First, because they do not know how, and second, because it is relevant only to the switch /Wp64 that is announced deprecated and will be removed.
Although the analyzer Viva64 (included into PVS-Studio) we are developing is not very good at handling templates either, it still works correctly and generates the expected warnings for this code, but relying on other rules. In particular, if the warning V101 is enabled, it generates the warning when unsigned type is cast to size_t because it might hide an error. Thus, Viva64 analyzer will generate the following:
std::vector<size_t> vs; std::vector<unsigned int> vi; size_t s; unsigned int i; vs[0] = s; vs[0] = i; //V101: Implicit assignment //type conversion to memsize type. vi[0] = s; //V103: Implicit type conversion //from memsize to 32-bit type. vi[0] = i; s = vs[0]; i = vs[0]; //V103: Implicit type conversion //from memsize to 32-bit type. s = vi[0]; //V101: Implicit assignment //type conversion to memsize type. i = vi[0];Still the analyzer may understand in some cases that some assignments are safe and reduce the number of false alarms. Here is an example:
std::vector<unsigned int> vi; for (size_t i = 0; i < 10; i++) vi[i] = i;The compiler generates the warning for this code:
warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data
But Viva64 analyzer takes into account that the value of the variable "i" lies within the range [0..10] and this code cannot cause an error. As a result, it does not generate any diagnostic warnings.
Комментариев нет:
Отправить комментарий