size() of vector gives type conversion warnings

Posted on 2004-11-07
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
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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

    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

    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

    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

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now