Here is a function-based converting iterator. It has all the proper typedefs for a forward iterator. We could upgrade it to support all of the tag properties of the incoming Base
iterator type if we chose:
template<
class Base,
class F,
class R=typename std::result_of<F(decltype(*std::declval<Base const&>()))>::type
>
struct convert_iterator:
std::iterator<std::forward_iterator_tag,typename std::decay<R>::type>
{
Base it;
F f;
template<class It, class Func>
convert_iterator(It&&base, Func&&func):it(std::forward<It>(base)),
// defaulted stuff:
convert_iterator()=default;
convert_iterator(convert_iterator const&)=default;
convert_iterator(convert_iterator &&)=default;
convert_iterator& operator=(convert_iterator const&)=default;
convert_iterator& operator=(convert_iterator &&)=default;
bool operator==(convert_iterator const&other) const {
return it == other.it;
}
bool operator!=(convert_iterator const&other) const { return !(*this==other); }
// a bit overkill, but rvalue and lvalue overrides for these:
R operator*() const& {
return f(*it);
}
R operator*() & {
return f(*it);
}
R operator*() const&& {
return std::move(f)(*std::move(it));
}
R operator*() && {
return std::move(f)(*std::move(it));
}
// normal pre-increment:
convert_iterator& operator++()& {
++it;
return *this;
}
// pre-increment when we are guaranteed not to be used again can be done differently:
convert_iterator operator++()&& {
return {std::next(std::move(it)), std::forward<F>(f)};
}
// block rvalue post-increment like a boss:
convert_iterator operator++(int)& {
return {it++, f};
}
};
a helper function to create them:
template< class Base, class F >
convert_iterator<typename std::decay<Base>::type,typename std::decay<F>::type>
make_convert_iterator(Base&& b, F&& f) { return {std::forward<Base>(b), std::forward<F>(f)}; }
Next I create a class that handles conversion. Specialization lets us dispatch differently for containers and scalars:
// for scalars:
template<class ToType,class=void>
struct converter {
template<class FromType>
ToType operator()(FromType&& from)const{ return std::forward<FromType>(from); }
};
// attempt at SFINAE test for container:
template<class ToContainer>
struct converter<ToContainer, (void)(
typename std::iterator_traits<
typename std::decay<decltype(std::begin(std::declval<ToContainer&>())>::type
>::value_type
)>
{
using std::begin; using std::end;
using R=std::iterator_traits<typename std::decay<decltype(begin(std::declval<ToContainer&>()))>::type>::value_type;
template<class FromType, class T=decltype(*begin(std::declval<FromType>())>
ToContainer operator()(FromType&& from) const {
auto sub_convert = [](T&& t)->R{
return converter<R>{}(std::forward<T>(t));
};
return {
make_convert_iterator(begin(std::forward<From>(from)), sub_convert),
make_convert_iterator(end(std::forward<From>(from)), sub_convert)
};
};
};
The action convert function is now a one-liner:
template<class ToType>
ToType convert(FromType&& from)
{
return converter<ToType>{}(std::forward<FromType>(from));
}