How to check if an iterator is output_iterator in C ++?

template<typename Iterator>
void put_value(Iterator pos, int n)
{
    static_assert(IsOutputIterator<Iterator>); 
    //
    // How to implement IsOutputIterator?
    //

    *pos = n;
}

std::iterator_traits<Iterator>::iterator_categoryDoes not help. For example: vector<int>::iteratorobviously a output_iterator, but std::iterator_traits<vector<int>::iterator>::iterator_categorywill return random_access_iterator, which may not be output_iterator, say a const_iterator.

Is there a viable way to check if an iterator is an output_iterator in C ++?

+4
source share
1 answer

- " ?" ++ , . , E o, - , *o = std::declval<decltype((E))>() .

++ 14, , :

template <class...> using void_t = void;

template <class, class, class = void>
constexpr bool is_output_iterator = false;

template <class I, class E>
constexpr bool is_output_iterator<I, E, void_t<
    typename std::iterator_traits<I>::iterator_category,
    decltype(*std::declval<I>() = std::declval<E>())>> = true;

++ , , , , - TS github OutputIterator<I, E>() .

+3

Source: https://habr.com/ru/post/1662476/


All Articles