Link to home
Start Free TrialLog in
Avatar of anasagasti
anasagasti

asked on

Convert a reverse_iterator to const_reverse_iterator

Using VC++6.0
Is it possible to convert a reverse_iterator to a const_reverse_iterator? I don't know if it's again a problem in this version of the STL

The following code in my case doesn't work:

main()
{
      typedef set<int> Sint;

      Sint s1;
      s1.insert(s1.end(), 1);
      s1.insert(s1.end(), 2);

      Sint::reverse_iterator rit = s1.rbegin();
      Sint::const_reverse_iterator crit(rit);

      cout << *crit << endl;

      return 0;
}

I obtain this error

Thanks

 error C2664: '__thiscall std::reverse_bidirectional_iterator<class std::_Tree<int,int,struct std::set<int,struct std::less<int>,class std::allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int>
 >::const_iterator,int,int const &,int const *,int>::std::reverse_bidirectional_iterator<class std::_Tree<int,int,struct std::set<int,struct std::less<int>,class std::allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::const_it
erator,int,int const &,int const *,int>(class std::_Tree<int,int,struct std::set<int,struct std::less<int>,class std::allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::const_iterator)' : cannot convert parameter 1 from 'class
 std::reverse_bidirectional_iterator<class std::_Tree<int,int,struct std::set<int,struct std::less<int>,class std::allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::iterator,int,int &,int *,int>' to 'class std::_Tree<int,int,
struct std::set<int,struct std::less<int>,class std::allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::const_iterator'
        No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
Avatar of Knut Hunstad
Knut Hunstad
Flag of Norway image

Why don't you just use:

Sint::const_reverse_iterator crit = s1.rbegin();

I'm trying to understand why you want to do it in the way you describe...
Avatar of anasagasti
anasagasti

ASKER

It was just an example.  In some context, you can have an implicit conversion from a reverse_iterator to a const_reverse_iterator. I wondered if that was possible. Apparently not with VC++6.0 and yes VC++2003. I know the example was absurd.
ASKER CERTIFIED SOLUTION
Avatar of Knut Hunstad
Knut Hunstad
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It compiles in 2003 but not in 6.0.
Thanks a lot for your help.
Wasn't really much, glad you thought it worth some points :-)