I want a mechanism to determine in compile time whether an iterator is reverse or not.
Iterator traits can only help with the category of an iterat
A simple method. It is easy to use and can determine if something is a reverse iterator or not, and can differentiate between const reverse iterators if needed.
#include
template
using IsRegRevIter = std::is_same;
template
using IsConstRevIter = std::is_same;
template
struct IsRevIter
{
const static bool value = IsRegRevIter::value
|| IsConstRevIter::value;
};
int main()
{
using Container = std::list;
Container myList = {1,2,3,4,5,6};
auto RI = myList.rbegin();
auto I = myList.begin();
std::cout << std::endl << std::boolalpha << IsRevIter::value; //true
std::cout << std::endl << std::boolalpha << IsRegRevIter::value; //true
std::cout << std::endl << std::boolalpha << IsConstRevIter::value; //false (it is not const).
std::cout << std::endl << std::boolalpha << IsRevIter::value; //false
return 0;
}
output:
false
true
false
false