how to detect if a type is an iterator or const_iterator

前端 未结 5 481
北荒
北荒 2020-12-31 07:24

I\'m wondering, if there is a way to check at compile time whether a type T of some iterator type is a const_iterator, or not. Is there some difference in the types that ite

5条回答
  •  盖世英雄少女心
    2020-12-31 07:57

    This is a bit hacky because you have to pass T itself but it works (template specialization, g++ 4.4.5):

    template
    struct is_const_iterator {
        enum {
            value = false
        };
    };
    
    template
    struct is_const_iterator {
        enum {
            value = true
        };
    };
    

    Use like this:

    typedef std::vector T;
    is_const_iterator::value           //is false
    is_const_iterator::value     //is true
    

提交回复
热议问题