0

I am learning c++. I have learned referencing allow us to avoid copying. So I tried to get an iterator of a vector as reference to overwrite its elements.

// simplified function
std::vector<int>::iterator& GetIterator(std::vector<int>& vec) {
    return vec.begin();  // compiler said that here is an error.
}

But compiler give me an error message and suggests to use const reference. I would not like to use const. Why cannot it get non-const reference? Or how to avoid copying of iterator? Thank you very much.

9
  • 3
    vec.begin() returns by value. So think about what that reference would refer to. Commented Jan 29, 2017 at 13:30
  • 1
    What is the exact error message you get? Commented Jan 29, 2017 at 13:31
  • Why do you want to avoid copying an iterator? Commented Jan 29, 2017 at 13:31
  • 1
    @mora For a vector, I'd expect the cost of an iterator to be exactly the same as the cost of a reference. Commented Jan 29, 2017 at 13:42
  • 3
    At this point im your C++ career, don't touch references except in function parameters. In particular, don't return references from functions, ever. Commented Jan 29, 2017 at 13:46

3 Answers 3

2

You can sometimes use a reference to avoid a copy - but here you need a copy. The result of vec.begin() is a temporary which ceases to exist when your function returns. If you want the value outside the function - and you do - then a copy is needed. (If you succeeded in forming a reference here it would be a "dangling reference", which is not a good thing.)

And in any case a lot of types are designed to be copied efficiently, and iterators are amongst those. And in fact the compiler can often avoid the copy for you, without you having to do any work at all - and this is one of those cases.

So the code without the reference is easier, correct, and already optimal.

Sign up to request clarification or add additional context in comments.

Comments

1

vec.begin(); return a temporary object and is r-value. You can't bind a l-value reference to a r-value. After returning from GetIterator function, that temporary object will be destroyed and your reference will be dangled.

Return by value (std::vector<int>::iterator) instead of reference. Iterators are designed to be copyable and cheap to copy. Also compilers use Return value optimization to avoid cost of coping objects.

2 Comments

Thank you for telling me why my code was wrong and how to do. Your instruction was clear and sharp.
@MRB return a temporary object and is r-value reference. It's not a rvalue reference, it is just a rvalue.
0

correct code

std::vector<int>::iterator GetIterator(std::vector<int>& vec) {
    return vec.begin();  // compiler said that here is an error.
}

your & is extra.

The iterator is like int* and gets a address.
And std::vector< type >::iterator is like that.
So the vector.begin() is like &int so you can not bind it to iterator&

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.