Re: C4267 with std::deque<size_t>

Tech-Archive recommends: Fix windows errors by optimizing your registry



<gast128@xxxxxxxxxxx> 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


.



Relevant Pages

  • How to switch off those damm warnings about unknows pragma
    ... > The thing I which for is a pragma to switch off the damm warning about not ... lots of spurious warning when compiling with ObjectAda. ... most of those compiled for more platform are with the same compiler ...
    (comp.lang.ada)
  • Re: Disable Compiler Waring
    ... I want to disable compiler warning C4482 (nonstandard extension used: ... enum 'enum' used in qualified name) ... #pragma warning ... I guess there are some compiler switch for it. ...
    (microsoft.public.vc.language)
  • Re: afxtempl.h compiler warnings
    ... Studio 6 to Visual Studio 2005. ... 'new' no previous #pragma push_macro for this identifier ... in the afxtempl.h header file, and also seven occurrences of: ...
    (microsoft.public.vc.mfc)
  • OpenGL, Watcom C and C++ (Yet another... ;-)
    ... Before deciding to ask You for help, I have spent many many hours of searching on the web because I got so many errors and not having experience with a modern compiler like this one, it baffled me and gave me many headaches. ... colconverted function type has different #pragma from original function type ...
    (comp.graphics.api.opengl)
  • Re: 64-bit warnings from Visual Studio supplied include file
    ... I'd even suggest reporting this to Microsoft. ... you could then turn the pragma off in stdafx.h after all the includes were done. ... While on the whole the warning would make sense, it is overkill for this particular ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)