The syntax of STL list::insert is as below –
iterator
insert(iterator __position, const value_type& __x);
Why a iterator instead of a reference to an iterator is being passed?
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.
An implementation would be allowed to pass the iterator by const reference instead of by value. There is no relevant semantic difference.
Passing by value is slightly more efficient in this case. The
iteratorobject contains a single pointer to a list element object on the heap. Remember that passing by reference is essentially passing by pointer, behind the scenes. (Although a reference is not the same as a pointer, when crossing an actual function call boundary, there is no other viable implementation.)So, passing by value means the pointer-to-heap gets passed, but passing by reference means the pointer-to-
iteratorgets passed, and then two indirections are required inside theinsertfunction. (This does in theory depend on the ABI, but anyway there cannot be a disadvantage to pass by value.)A quick illustration of passing semantics:
http://ideone.com/2mC07
If it weren’t passing pointers, there is no way for
same_params< int const & >to work.