jeudi 26 février 2015

Did I make a dangling reference?


Normally, I'd omit the std::vector but that would require me to make my function take a const int&. What I want to know is since there's a temporary vector if I have a dangling reference. Is it safe to modify its elements?



#include <iostream>
#include <vector>

void foo(int& i)
{
i = 42;
}

void foo(int&& i)
{
std::cout << "int&&";
}

int main()
{
for (auto&& i : std::vector<int>{1, 2, 3})
{
foo(std::forward<decltype(i)>(i));
}
}



Aucun commentaire:

Enregistrer un commentaire