This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

FAQ Entry



Jeff & Joe --

  Here's another entry for the egcs FAQ, which I thought Joe might
want in his FAQ as well:

Q.  I can say:

    #include <vector>
    ...
    vector<double> vd(10, 3.0);

    to create a vector of 10 doubles, each initialized to 3.0, but
    I can't say:

    #include <vector>
    ...
    vector<int> vi(10, 3);

    to create a vector of 10 ints, each initialized to 3.  Why not?

A.  There are two constructors of interest for vector, as specified
    in the Standard:

    template <class InputIterator>
    vector<InputIterator, InputIterator);

    vector(size_type, const T&);

    By the rules given in the Stanard, the best match for the
    arguments 10 and 3 is the InputIterator version, even though
    10 and 3 are not, of course, iterators.  So, the code doesn't work
    as expected.  You can write:
    
    vector<int> vi((vector<int>::size_type) 10, 3);
   
    or, since the size_type is unsigned, 

    vector<int> vi(10U, 3);

    to do what you expect.

-- 
Mark Mitchell		mmitchell@usa.net
Stanford University	http://www.stanford.edu



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]