Determine if a (c++) iterator is reverse

后端 未结 2 1062
感情败类
感情败类 2020-12-20 10:09

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

2条回答
  •  时光说笑
    2020-12-20 10:46

    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

提交回复
热议问题