PDA

View Full Version : C++ STL vector: handling an ambiguity



schultz
July 29th, 2007, 08:18 PM
I just had a C++ doubt and found nowhere else to post. It is about the STL vector.

It is know this code is correct:

int buff[100];
vector<int> A(10, 18 ) ;
vector<int> B(buff, buff+100);

This should leave A with 10 copies of 18 and B with the contents of buff.

The problem is, analysing the code of the vector data structure, I find something like:

vector(size_t, const _Tp&);
...
template<typename _InputIterator> vector(_InputIterator, _InputIterator);

This, of course, if you ignore allocator issues. My doubt is why this code is not ambiguous, since if I call vector(10, 18 ), it fits in both prototypes? This is, since if _InputIterator = int and _Tp = int we should have two prototypes fitting vector(int, int), there should be an ambiguity.

Can some C++ guru help me with this?

thumper
July 29th, 2007, 10:32 PM
When evaluating which function to call, a non-template matching function is chosen over any templated function.

schultz
July 30th, 2007, 01:37 AM
Thanks for the hint, it helps a lot. Thanks indeed.