How to check if an arbitrary type is an iterator?

前端 未结 3 580
伪装坚强ぢ
伪装坚强ぢ 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

    I implemented this one some time ago:

    template 
    struct is_iterator {  
        template 
        static char test(typename std::iterator_traits::pointer* x);
    
        template 
        static long test(U* x);
    
        static const bool value = sizeof(test(nullptr)) == 1;
    };
    

    It compiles fine using your example. I can't test it on VC though.

    Demo here.

提交回复
热议问题