size() of vector gives type conversion warnings

Hi,

I recently inherited this library (the source code) which uses STL vectors. I'm running it in VC++ v7.0.9466 and .NET framework 1.0.3705, and everytime i compile, i get a whole bunch of warning messages about "conversion from 'size_t' to 'blah', possible loss of data."

For example, the code below will produce 2 such warnings.

#include <vector>
void main() {
      std::vector<unsigned int> temp(5);

      int foo = temp.size();
      unsigned bar = temp.size();
}

I understand that the size() method returns a type of size_t, which explains the warning messages.  But is there a way to make VC++ detect that for example it is actually an unsigned and not produce unnecessary warnings? Even though the warnings are harmless, I just don't like getting tons of warning messages everytime I compile.

Thanks.
SelconAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
size_t is in fact an unsigned integer, so you can use:

unsigned int foo = temp.size();

But, why not to use this instead?

size_t foo = temp.size();

Anyway, to avoid an specific warning message you can use a pragma directive in the top of your files:

#pragma warning( disable : 9999 )

where 9999 is a warning code.
0
drichardsCommented:
No, size_t is defined as 'unsigned __int64', so '(un)signed int' is in fact too short.  You need to put in explicit casts or use size_t to store the results to get rid of the warnings.  To cast:

#include <vector>
void main() {
     std::vector<unsigned int> temp(5);

     int foo = static_cast<int>(temp.size());
     unsigned bar = static_cast<unsigned>(temp.size());
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SelconAuthor Commented:
is that the best way to deal with these warnings?

so in all projects that use STL vector, there will be static_cast<> all over the place, especially in loops across elements in a vector, etc.? or there will be size_t all over the place i suppose.


0
drichardsCommented:
Yes, without disabling the warning, those are your choices.  

I try to use size_t where I can and then use static_cast to handle cases where the target value is already a smaller integer type.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.