Is it possible to infer the contained type for std :: insert_iterator?

I have a function that expects a template type of an iterator.

An iterator is being dereferenced to check for a repeating type.

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 some standard types of iterators, such as std::insert_iterator , define *i as just a reference to i .

That is, sizeof(*i) is the size of the iterator itself; same as sizeof(i) or sizeof(***i)

Is there a universal way (supporting C ++ 03) for determining the size or type of objects that are repeated by any standard iterator?

+4
source share
2 answers

I'm not sure why you would like the value_type OutputIterator, because there is no way to extract the value from the Output Iterator. However, the three plug-in adapter iterators define value_type as void and provide an element of type container_type , so you can return to value_type T::container_type if value_type of T rotated to be void .

(In the " value_type " section, I really mean std::iterator_traits<T::container_type>::value_type and std::iterator_traits<T>::value_type .)

Or you could just not try to use output iterators, as if they mattered :)

Edit: SFINAE is not required: (even without C ++ 11 symmetry)

 template<typename U, typename T> struct helper {typedef U type;}; // ostream*_iterator handling courtesy Drew Dormann template <typename T, typename charT, typename traits> struct helper<void, std::ostream_iterator<T, charT, traits> > {typedef T type;}; template <typename charT, typename traits> struct helper<void, std::ostreambuf_iterator<charT, traits> > {typedef charT type;}; // std::raw_storage_iterator still needs an override // as well as any non-standard output iterators which don't define a container_type. template<typename T> struct helper<void, T> {typedef typename std::iterator_traits<typename T::container_type>::value_type type;}; typedef<typename It> struct my_value_type : public helper<typename std::iterator_traits<It>::value_type, It> {}; 
+2
source

Here is what for iterator_traits .

 typedef typename std::iterator_traits<Iterator>::value_type type; const std::size_t type_size = sizeof(type); 

Edit: this should not work for all output iterators.

+5
source

All Articles