c++ - Comparing two iterators of a different type in a template function -
no c++11 or boost :(
i have function following signature.
template<class input_itr, class output_itr> void dowork(const input_itr in_it, const input_itr in_it_end, output_itr out_it, output_itr out_it_end, context_data)
normally complex processing takes place between input , output.. no-op required, , data copied. function supports in-place operations if input , output data types same. have code.
if (noop) { if (in_it != out_it) { copy(in_it, in_it_end, out_it); } }
if in-place operation has been requested (the iterator check), there no need copy data.
this has worked fine until call function iterators different data types (int32 int64 example). complains iterator check because incompatible types.
error c2679: binary '!=' : no operator found takes right-hand operand of type 'std::_vector_iterator<std::_vector_val<std::_simple_types<unsigned __int64>>>
this has left me bit stumped. there easy way perform check if data types same, perform copy if different types?
thanks
you can extract test pair of templates; 1 matching types, 1 non-matching types.
template <class t1, class t2> bool same(t1 const &, t2 const &) {return false;} template <class t> bool same(t const & a, t const & b) {return == b;}
beware can give confusing results when used types you'd expect comparable. in c++11 (or boost, or lot of tedious mucking around templates) extend compare different types when possible; that's beyond need here.
also, note you're relying on formally undefined behaviour, since iterators on different underlying sequences aren't required comparable. there no way tell iterators whether case.
Comments
Post a Comment