Re: C4267 with std::deque<size_t>
 
<gast128@hotmail.com> wrote:
Dear all,
we have encountered a very strange warning in a very 
specific contex:
using a std::deque with size_t on VStudio 2003, warning 
level 4 and an
exported bool vector, i.e.:
//1.cpp
#include <deque>
#define CLASS_EXPORT  __declspec(dllexport)
//for explicit (DLL) instantiation of vector's:
#pragma warning(push)
#pragma warning(disable: 4251)
//warning C4251: ... needs to have dll-interface to be 
used by clients,
(TODO: can be dangerous however)
#include <vector>
#pragma warning(pop)
//explicit (DLL) instantiation
template class CLASS_EXPORT std::vector<bool>;
int main()
{
  //std::deque<unsigned> tmp1;
  std::deque<size_t>   tmp;
  tmp.resize(3);
  return 0;
}
A compiler bug I presume?
Not exactly. I'd call it compiler peculiarity. I can 
reproduce it with VC2005, as well. Explicit instantiation of 
std::vector<bool> is enough to trigger the C4267 warning. I 
don't know why instantiation of std::vector<bool> triggered 
this, however I have explanation to C4267 warning.
C4267 warns that `size_t' being converted to `unsigned int'. 
It's 64-bit compatibility warning. Under 64-bit Windows 
`size_t' is 64 bits while `unsigned int' is still 32 bits. 
However, under 32-bit Windows `size_t' is typedef'ed as 
`unsigned int'. I think that when std::deque<size_t> is 
instantiated, then it happens in two stages (or more) where 
one stage already recognizes `size_t' as `unsigned int', 
while other still sees `size_t'. So, in one place it's 
already `unsigned int' and in the other it's `size_t'. 
Compiler cannot predict that in all places it will become 
appropriate size type eventually, hence it warns.
As a quick solution you can disable Win64 portability issues 
detection (/Wp64 flag) for the project. Alternatively you 
can disable C4267 warning explicitly:
    #pragma warning(disable: 4267)
    #include <vector>
    #pragma warning(default: 4267)
HTH
Alex