How to check if an arbitrary type is an iterator?

前端 未结 3 570
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 10:48

This is an exact duplicate of this question, except that accepted answer is wrong, so I ask it again:

How do you correctly check to see if

3条回答
  •  [愿得一人]
    2020-12-05 11:31

    How about something like this?

    template
    struct is_iterator
    {
       static constexpr bool value = false;
    };
    
    template
    struct is_iterator::value_type, void>::value>::type>
    {
       static constexpr bool value = true;
    };
    

    example:

    #include 
    #include 
    #include 
    
    template
    struct is_iterator
    {
       static constexpr bool value = false;
    };
    
    template
    struct is_iterator::value_type, void>::value>::type>
    {
       static constexpr bool value = true;
    };
    
    int main()
    {
       static_assert(!is_iterator::value, "ass");
       static_assert(is_iterator::value, "ass");
       static_assert(is_iterator::iterator>::value, "ass");
    }
    

    http://liveworkspace.org/code/7dcf96c97fd0b7a69f12658fc7b2693e

提交回复
热议问题