I have a function that expects a templated iterator type.
It currently dereferences the iterator to inspect the type being iterated.
template < typename Iterator >
void func( Iterator i )
{
// Inspect the size of the objects being iterated
const size_t type_size = sizeof( *i );
...
}
I recently discovered that several of the standard iterator types, such as std::insert_iterator define *i as simply a reference to i.
That is, sizeof(*i) is the size of the iterator itself; the same as sizeof(i) or sizeof(***i)
Is there a universal way (supporting C++ 03) to determine the size or type of objects being iterated by any standard iterator?
I’m not sure why you would want the
value_typeof an OutputIterator, because there is no way to extract a value from an Output Iterator. However, the three insert iterator adaptors all definevalue_typeto bevoidand provide acontainer_typetype member, so you could fall back to thevalue_typeofT::container_typeif thevalue_typeofTturns out to bevoid.(By “
value_typeof” I really meanstd::iterator_traits<T::container_type>::value_typeandstd::iterator_traits<T>::value_type.)Or you could just not try to use Output Iterators as though they had values 🙂
Edit: SFINAE isn’t necessary: (even without C++11 niceness)