Re: Correct usage of std::vector?
On Feb 15, 1:07 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
c...@mailvault.com wrote:
[...]
Formally, the standard requires it to work. Whether this is
intentional or not is another question---it may be an error in
the standard. (On the other hand: the standard has guaranteed
it, so it's rather hard to say that in fact, it is illegal, even
if the guarantee wasn't intentional.)
The constructor that gets invoked it
std::vector<T> ( size_type n, T const & t )
No. The constructor which gets invoked is an instantiation of:
template< InputIterator >
vector::vector( InputIterator begin, InputIterator end ) ;
Instantiated with InputIterator == int, this is an exact
match; the one you propose requires a conversion of int to
size_t.
This is a good example of why C++ compilers should have an
option to show what they're generating for an instantiation.
Both of you are experienced and intelligent and yet at least
one of you is wrong.
The wrong one would be me. I re-checked the standard, and
James is absolutely correct. I missed that part (in fact, I
missed it earlier, too; and now I have to fix my vector
implementation).
It helps to have discussed the issues with someone who has
actually implemented the library. It certainly wouldn't have
occured to me on my own.
An obvious test case for the library is something like:
std::vector< int > v( 5, 10 ) ;
Without the special casing, this fails (which tends to surprise
at first). I think that in some of the initial, pre-standard
implementations of the library, it also failed; that you had to
write:
std::vector< int > v( static_cast< size_t >( 5 ), 10 ) ;
.. The committee felt that this would be too embarassing, and so
added the special case.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34