繁体   English   中英

如何检测类型是迭代器还是const_iterator

[英]how to detect if a type is an iterator or const_iterator

我想知道,如果有一种方法可以在编译时检查某个迭代器类型的类型T是否为const_iterator。 迭代器和const迭代器之间的迭代器定义的类型(value_type,pointer,...)有什么不同吗?

我想实现这样的目标:

typedef std::vector<int> T;

is_const_iterator<T::iterator>::value       // is false
is_const_iterator<T::const_iterator>::value // is true

C ++ 03解决方案:

由于答案似乎都没有,这是我与GCC合作的尝试:

template<typename T>
struct is_const_pointer { static const bool value = false; };

template<typename T>
struct is_const_pointer<const T*> { static const bool value = true; };

template <typename TIterator>
struct is_const_iterator
{
    typedef typename std::iterator_traits<TIterator>::pointer pointer;
    static const bool value = is_const_pointer<pointer>::value;
};

例:

int main()
{
    typedef std::vector<int>::iterator it_type;
    typedef std::vector<int>::const_iterator const_it_type;

    std::cout << (is_const_iterator<it_type>::value) << std::endl;
    std::cout << (is_const_iterator<const_it_type>::value) << std::endl;
}

输出:

0
1

在线演示: http//ideone.com/TFYcW

C ++ 11

template<class IT, class T=decltype(*std::declval<IT>())>    
constexpr bool  
is_const_iterator() {   
        return  ! std::is_assignable <
                decltype( *std::declval<IT>() ),
                T   
        >::value;
}

至少在gcc上工作的一种方法是通过引用 typedef:

struct true_type { };
struct false_type { };

template<typename T>
struct is_const_reference
{
    typedef false_type type;
};

template<typename T>
struct is_const_reference<T const &>
{
    typedef true_type type;
};

template<typename T>
struct is_const_iterator
{
    typedef typename is_const_reference<
        typename std::iterator_traits<T>::reference>::type type;
};

您可以使用验证它是否有效

inline bool test_internal(true_type)
{
    return true;
}

inline bool test_internal(false_type)
{
    return false;
}

template<typename T>
bool test(T const &)
{
    return test_internal(typename is_const_iterator<T>::type());
}

bool this_should_return_false(void)
{
    std::list<int> l;
    return test(l.begin());
}

bool this_should_return_true(void)
{
    std::list<int> const l;
    return test(l.begin());
}

如果具有足够高的优化级别,则应减少最后两个函数以return false; return true; , 分别。 至少他们为我做了。

使用C ++ 11,新标准头<type_traits>提供了std::is_const<T> ,因此可以简化Nawaz的解决方案:

template<typename Iterator>
struct is_const_iterator
{
    typedef typename std::iterator_traits<Iterator>::pointer pointer; 
    static const bool value = 
        std::is_const<typename std::remove_pointer<pointer>::type>::value;
};

这有点hacky,因为你必须传递T本身,但它的工作原理(模板专业化,g ++ 4.4.5):

template<typename T, typename S>
struct is_const_iterator {
    enum {
        value = false
    };
};

template<typename T>
struct is_const_iterator<T, typename T::const_iterator> {
    enum {
        value = true
    };
};

使用这样:

typedef std::vector<int> T;
is_const_iterator<T, T::iterator>::value           //is false
is_const_iterator<T, T::const_iterator>::value     //is true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM