Check if iterator's type is reverse_iterator

做~自己de王妃 提交于 2019-12-06 18:27:41

问题


Is there a way to check if iterator passed as arg to fnc is reverse_iterator? Are there any iterator traits function I could use?


回答1:


It's trivial to write with a partial specialization:

#include <iterator>
#include <type_traits>

template<typename Iter>
  struct is_reverse_iterator
  : std::false_type { };

template<typename Iter>
  struct is_reverse_iterator<std::reverse_iterator<Iter>>
  : std::true_type { };

Although as pointed out below, this doesn't handle the (IMHO unlikely) case of a "reverse-reverse" iterator. The slightly less trivial version in Bathsheba's answer handles that case correctly.




回答2:


Some code I use in production:

#include <iterator>
#include <type_traits>
template<typename I>
struct is_reverse_iterator : std::false_type
{
};

template<typename I>
struct is_reverse_iterator<std::reverse_iterator<I>>
: std::integral_constant<bool, !is_reverse_iterator<I>::value>
{
};


来源:https://stackoverflow.com/questions/35408762/check-if-iterators-type-is-reverse-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!