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
One method that works at least on gcc is via the reference typedef:
struct true_type { };
struct false_type { };
template
struct is_const_reference
{
typedef false_type type;
};
template
struct is_const_reference
{
typedef true_type type;
};
template
struct is_const_iterator
{
typedef typename is_const_reference<
typename std::iterator_traits::reference>::type type;
};
You can verify that it works by using
inline bool test_internal(true_type)
{
return true;
}
inline bool test_internal(false_type)
{
return false;
}
template
bool test(T const &)
{
return test_internal(typename is_const_iterator::type());
}
bool this_should_return_false(void)
{
std::list l;
return test(l.begin());
}
bool this_should_return_true(void)
{
std::list const l;
return test(l.begin());
}
With a sufficiently high optimization level, the last two functions should be reduced to return false;
and return true;
, respectively. At least they do for me.