For example, std::vector<int>::iterator it = --(myVec.end());. This works in GCC 4.4 but I have heard a rumor that it’s not portable.
For example, std::vector<int>::iterator it = –(myVec.end()); . This works in GCC 4.4 but I
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It will only work if
std::vector<int>::iteratoris an object type withoperator++a member function. If it’s a scalar type (e.g.int *), oroperator++is a non-member function, it will fail.Non-const non-static member functions can be called on temporary objects (since they have non-
constobject type, per 9.3.2p3), but an lvalue reference parameter in a non-member function cannot bind to a temporary (13.3.3.1.4p3).This means that it’s nothing to do with the compiler, but rather the standard library; as you’ve observed libstdc++ is implemented with
std::vector<int>::iteratoran object type with memberoperator++, but your code could easily be compiled with the same compiler and a different standard library wherestd::vector<int>::iteratorisint *, in which case it would fail.std::vector,std::arrayandstd::stringare the only container templates that can sensibly be implemented with scalar (pointer) iterators, but that doesn’t mean that calling++on other containers’ iterators is safe; they could have non-memberoperator++asTabove.To make an iterator to the before-the-end element, use
std::prev:std::prevandstd::nextare new in C++11, but are easily implementable in C++03.