Why isn’t first passed as a reference and const as well?
template <typename Iterator>
int distance(Iterator first, const Iterator & last) {
int count;
for ( ; first != last; first++)
count++;
return count;
}
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 cannot be
constbecause it is incremented inside the function, and it is not passed by reference because it probably makes no sense to do so for the caller.Furthermore, if it were non-const reference, it would not be possible to use a temporary. For example, you wouldn’t be able to do tis: