Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


size() of vector gives type conversion warnings

Posted on 2004-11-07
Medium Priority
Last Modified: 2010-04-24

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.

Question by:Selcon
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12519976
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.
LVL 19

Accepted Solution

drichards earned 400 total points
ID: 12520066
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());

Author Comment

ID: 12521336
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.

LVL 19

Expert Comment

ID: 12529026
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.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question