how to detect if a type is an iterator or const_iterator
c++, iterator, metaprogramming, stl, templates
Solution
C++03 Solution:
As none of the answer seems correct, here is my attempt which is working with 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;
};
Example:
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;
}
Output:
0
1
Online Demo : http://ideone.com/TFYcW
Problem
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 iterators define (value_type, pointer, ...) between iterators and const iterators? I would like to achieve something like this: ``` typedef std::vector<int> T; is_const_iterator<T::iterator>::value // is false is_const_iterator<T::const_iterator>::value // is true ```