Prev: Most vexing parse (was: Why you should never use a const& parameter to initialize a const& member variable!)
Next: Compile time hooks, like: AClass<SomeType,Hooks<FunctorOne,FunctorTwo,...> >
From: Daniel Krügler on 10 Feb 2010 08:19 On 10 Feb., 20:28, Edward Diener <eldiener_no_spam_h...(a)tropicsoft.com> wrote: > Daniel Kr�gler wrote: > > On 8 Feb., 00:01, Edward Diener <eldiener_no_spam_h...(a)tropicsoft.com> > > wrote: > >> In the two forms of vector::erase: > > >> iterator erase(iterator position); > >> iterator erase(iterator first, iterator last); > > >> can the 'iterator' mentioned be a vector::const_iterator, > >> vector::reverse_iterator, or vector::const_reverse_iterator as well as a > >> vector::iterator ? > > > Additional to the comments from Mathias and achp > > it is worth noting that from C++0x on you can indeed > > use vector::const_iterator as function arguments > > here, because they are just "position values", which > > are not affected by the iterator capability to > > modify the referenced data. > > Are you saying that in C++0x a const_iterator means that one can not > modify the value to which the iterator refers, as opposed to modifying > the sequence itself ? No, and I don't know how you could conclude that from my answer ;-) > In that case will the sequences change, so that > possible sequence member functions which modify the sequence, and use an > iterator to specify a position in the sequence, will now have > const_iterator versions of the modifying member functions ( such as > vector::erase ) ? The behavior of erase will not be different, more specifically, the function will still return a value of type iterator (not const_iterator). The function arguments of erase have never been used to modify the elements *directly*. They are simple position markers, which just "point" to the location(s) that should be erased. Note that std::string has an erase overload: basic_string& erase(size_type pos = 0, size_type n = npos); where we can see the position character more clearly. This is not different for other containers, and for std::vector we can write the transformation between iterator and index in one line: vector::size_type pos = first - begin(); vector::size_type n = (last - first); Returning to your erase arguments: The fact that erase will now accept const_iterator does not make any observable difference for you, because your current iterator arguments will implicitly convert to the const_iterator value. Wherever you had used a non-const_iterator (aka: iterator), you still have the same way of modifying the pointed-to elements as before. With C++0x you will be able to do something that wasn't (portably) possible in C++03: You can now indeed provide const_iterator arguments to the erase functions (and some other functions which use it's iterator arguments for pure positional reasons) HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |