What are the differences between for (auto&& i : v)
and for (auto& i : v)
?
I understand that auto&&
is a universal reference. I clearly know auto &&
could return references of rvalues and lvalues. Does auto &
only return a reference of lvalues. Am I right? Are there some potential problems with for (auto& i : v)
?
Here is some related code:
#include <iostream>
#include <vector>
#include <typeinfo>
int main()
{
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (auto& i : v)
{
std::cout << i << ' ';
std::cout << typeid(i).name() << std::endl;
}
for (auto&& i : v)
{
std::cout << i << ' ';
std::cout << typeid(i).name() << std::endl;
}
}