Skip to content

Instantly share code, notes, and snippets.

@zmij
Created November 24, 2018 13:31
Show Gist options
  • Save zmij/329005f5d06369243988ef6301f37616 to your computer and use it in GitHub Desktop.
Save zmij/329005f5d06369243988ef6301f37616 to your computer and use it in GitHub Desktop.
Detect output iterator value type
#include <iterator>
template <typename T>
struct output_iterator_value {
using type = void;
};
template <typename Container>
struct output_iterator_value<std::insert_iterator<Container>> {
using type = typename Container::value_type;
};
template <typename Container>
struct output_iterator_value<std::back_insert_iterator<Container>> {
using type = typename Container::value_type;
};
template <typename Container>
struct output_iterator_value<std::front_insert_iterator<Container>> {
using type = typename Container::value_type;
};
template <typename T, typename CharT, typename CharTraits>
struct output_iterator_value<std::ostream_iterator<T, CharT, CharTraits>> {
using type = T;
};
template <typename T>
struct iterator_value {
using iterator_traits = std::iterator_traits<T>;
using type = typename std::conditional<
std::is_same<typename iterator_traits::iterator_category,
std::output_iterator_tag>::value,
typename output_iterator_value<T>::type,
typename iterator_traits::value_type
>::type;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment