I don't understand why this code doesn't compile.
The error is with std::iterator_traits. Compiler says it can't find iterator_category for std::vector which is fine. Therfore the second template should be excluded from matching. Somehow, it casues an error. Any ideas?
Don't write that it's not useful. I know it. I'm only testing.
#include <vector>
#include <algorithm>
#include <iterator>
template <typename Cont, typename Cont2>
typename Cont2::iterator copy(const Cont& c, Cont2& dest)
{
//return std::copy(c.begin(), c.end(), std::back_inserter(dest));
std::copy(c.begin(), c.end(), std::back_inserter(dest));
return dest.end();
}
template <typename Cont, typename It>
It copy(const Cont& c, It dest, typename std::iterator_traits<It>::pointer = 0)
{
return std::copy(c.begin(), c.end(), dest);
}
int main()
{
std::vector<int> a, b;
copy(a, b);
}
Comeau errors:
"stl_iterator_base.h", line 110: error: class
"std::vector<int, std::allocator<int>>" has no member
"iterator_category"
typedef typename _Iterator::iterator_category iterator_category;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with
_Iterator=std::vector<int, std::allocator<int>>]" at line
21 of "ComeauTest.c"
"ComeauTest.c", line 21: error: more than one instance of overloaded function "copy"
matches the argument list, the choices that match are:
function template "Cont2::iterator copy(const Cont &, Cont2 &)"
function template "It copy(const Cont &, It,
std::iterator_traits<_ForwardIter2>::pointer)"
The argument types that you used are: (std::vector<int, std::allocator<int>>,
std::vector<int, std::allocator<int>>)
copy(a, b);
^
[Edited by - rozz666 on May 29, 2009 7:11:36 AM]