This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
FAQ Entry
- To: egcs at cygnus dot com
- Subject: FAQ Entry
- From: Mark Mitchell <mmitchell at usa dot net>
- Date: Tue, 6 Jan 1998 23:29:33 GMT
- Cc: "Jeffrey A. Law" <law at schirf dot cs dot utah dot edu>, Joe Buck <jbuck at synopsys dot com>
- Reply-To: mmitchell at usa dot net
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