http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49022
--- Comment #18 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-06-14 12:51:45 UTC --- (In reply to comment #16) > Then, the snippet: > > std::valarray<int> v1{ 0, 1, 2, 3, 4 }, v2{ 5, 6, 7, 8, 9}; > > for (int i : v1 + v2) > { std::cout << i << std::endl; } > > compiles but then misbehaves at compile-time, per valgrind too: apparently we > don't have a proper range. Note that the following misbehaves exactly in the > *same way*, and this appears to support my conjecture that we have a problem > somewhere with two different temporary _Expr passed to the begin and end > overloads: > > for (auto b = std::begin(v1 + v2), e = std::end(v1 + v2); b < e; ++b) > { std::cout << *b << std::endl; } Hmm, then range-based for is implemented incorrectly, I'll take a look. It should be identical to auto&& range = v1 + v2; for (auto b = std::begin(range), e = std::end(range); b != e; ++b) { ... } (see 6.5.4 [stmt.ranged]) of course the same problem is present for any temporary expression: std::string s = "hello" for (auto b = std::begin(s+"!"), e = std::end(s+"!"); b != e; ++b) but it should work correctly as: for (auto c : s+"!")