c++ - How does overloaded template function selection (pattern matching) work in std::vector insert? -



c++ - How does overloaded template function selection (pattern matching) work in std::vector insert? -

consider next declarations of std::vector (taken cplusplus - eastl has same declarations)

iterator insert ( iterator position, const t& x ); void insert ( iterator position, size_type n, const t& x ); template <class inputiterator> void insert ( iterator position, inputiterator first, inputiterator lastly );

if type

somevector.insert(somevector.begin(), 10, 90);

how not confused (by compiler) lastly overload, 10 , 90 ints , inputiterator's type taken int instead of alternative take 10 size_type , 20 const int&?

now "not" because implementing vector container (learning purposes) , in case above mentioned call, 3rd overload selected compiler rather sec overload , consequently fails compile. if remove 3rd overload things seem fine.

does have lastly overload calling (overloaded functions iterator traits)? if so, if assume iterators raw pointers (although in case using same declaration, means have overload #3 template expects iterator... although expects wrong word here because in end can , in case, interpreted int me , fails compile) how create sure compiler selects proper function?

out of curiosity, had @ gcc sources:

template<typename _inputiterator> void insert(iterator __position, _inputiterator __first, _inputiterator __last) { // check whether it's integral type. if so, it's not iterator. typedef typename std::__is_integer<_inputiterator>::__type _integral; _m_insert_dispatch(__position, __first, __last, _integral()); }

later...

// _glibcxx_resolve_lib_defects // 438. ambiguity in "do right thing" clause template<typename _integer> void _m_insert_dispatch(iterator __pos, _integer __n, _integer __val, __true_type) { _m_fill_insert(__pos, __n, __val); } // called range insert implement [23.1.1]/9 template<typename _inputiterator> void _m_insert_dispatch(iterator __pos, _inputiterator __first, _inputiterator __last, __false_type) { typedef typename std::iterator_traits<_inputiterator>:: iterator_category _itercategory; _m_range_insert(__pos, __first, __last, _itercategory()); }

it seems indeed worried ambiguity arise, explicit utilize of typetraits , overloading used check whether template matched integral type or not.

c++ templates function-overloading

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -