In this blog post, Eric Niebler states that:
What is wrong with std::begin and std::end? Surprise! they are not memory safe. Consider what this code does:
extern std::vector<int> get_data(); auto it = std::begin(get_data()); int i = *it; // BOOM
std::begin has two overloads for const and non-const lvalues. Trouble is, rvalues bind to const lvalue references, leading to the dangling iterator it above.
I'm having trouble understanding his point and why it
is a dangling reference.
Could someone explain?
auto it = get_data().begin();
. That code has the same problem I think, so what point is the author is making? – johnranges::begin
is better thanstd::begin
, he isn't claiming anything about.begin
. – Marc Glissestd::begin()
is exactly as memory-safe as the parameter you pass to it, and not inherently unsafe solely in and of itself. That's just me, though. – Justin Time - Reinstate Monica