Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reference and iterators

What happens if std::vector::begin() is called but the returned iterator is assigned to a reference? Why does it work and where is the iterator value stored?

std::vector<int> v;
auto a = v.begin(); //I assume iterator is stored on the stack in variable "a".
auto& b = v.begin(); //What happens here?
like image 415
Tom Avatar asked Jul 22 '26 02:07

Tom


1 Answers

This line

auto& b = v.begin();

is an error, because v.begin() returns a temporary, and you can't bind a temporary to a non-const reference.

like image 61
Vaughn Cato Avatar answered Jul 23 '26 14:07

Vaughn Cato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!