I’m somewhat trying to implement a kind of copy operator.
The aim is to have two classes: one that browse a container, and one that do something with it. The Browse class also maintain (for some reason) an iterator on the ouput container, and the other one can compute an increment with it.
Unfortunately, the compiler seems to be unable to convert a back_insert_iterator to the output iterator. Why?
#include <iostream>
#include <iterator>
#include <vector>
typedef std::vector<int> Vec;
// An operator that copy an item onto another
template< class TIN, class TOUT >
class DoCopy
{
TOUT operator()( const typename TIN::iterator i_in, const typename TOUT::iterator i_out )
{
const typename TOUT::iterator i_incr = i_out;
(*i_incr) = (*i_in);
std::advance( i_incr, 1 );
return i_incr;
}
};
// The class that iterate over a container, calling an operator for each item
template< class TIN, class TOUT >
class Browse
{
public:
// We keep a reference to the operator that really do the job
DoCopy<TIN,TOUT> & _do;
Browse( DoCopy<TIN,TOUT> & op ) : _do(op) {}
// Iterate over an input container
TOUT operator()(
const typename TIN::iterator in_start,
const typename TIN::iterator in_end,
const typename TOUT::iterator out_start
)
{
TOUT i_out = out_start;
for( TIN i_in = in_start; i_in != in_end; ++i_in ) {
// it is not shown why here, but we DO want the operator to increment i_out
i_out = _do(i_in, i_out);
}
}
};
int main()
{
// in & out could be the same type or a different one
Vec in;
Vec out;
DoCopy<Vec,Vec> do_copy;
Browse<Vec,Vec> copy(do_copy);
std::back_insert_iterator< Vec > insert_back(out);
// Here, g++ cannot find the corresponding function :
copy( in.begin(), in.end(), insert_back );
}
g++ fail to compile with the following errors:
$ g++ test.cpp && ./a.out
test.cpp: In function ‘int main()’:
test.cpp:54:49: erreur: no match for call to ‘(Browse<std::vector<int>, std::vector<int> >) (std::vector<int>::iterator, std::vector<int>::iterator, std::back_insert_iterator<std::vector<int> >&)’
test.cpp:22:11: note: candidate is:
test.cpp:30:18: note: TOUT Browse<TIN, TOUT>::operator()(typename TIN::iterator, typename TIN::iterator, typename TOUT::iterator) [with TIN = std::vector<int>, TOUT = std::vector<int>, typename TIN::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, typename TOUT::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
test.cpp:30:18: note: no known conversion for argument 3 from ‘std::back_insert_iterator<std::vector<int> >’ to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’
Here is the main source of the problem:
std::back_insert_iterator< V<T> >andstd::vector<T>::iteratoraren’t directly related in their inheritance tree:std::vector<T>::iteratoris a__normal_iterator<T>(no other super class) (look atstl_vector.hforstd::vector<T>::iteratorandstl_iterator.hfor__normal_iterator)std::back_insert_iterator< V<T> >is an iterator (no other super class) (look atstl_iterator.hforstd::back_insert_iteratorandstl_iterator_base_types.hforstd::iterator).They can’t be converted in any direction.
Hence, the second template argument should directly be the
std::back_insert_iteratoror theiterator<>with the good first parameter indicating that’s an output operator.By
std::advance( iterator, 1 ), I assume you meant++iterator, which is the standard way to go to the next element for iterators.Furthermore, out iterators shouldn’t be
const, otherwise they don’t implement the affectationoperator=.Line 38, the
i_inshould be of typetypename TIN::iteratorand notTIN.The Browse
operator()must also return the out iterator.The final code looks like this:
Thanks to clang++ which makes C++ errors more clear.