
12 Mar
2011
12 Mar
'11
3:12 a.m.
Hello, I am experiencing some strange behaviour when using adaptors with a range-based for loop. Here's what I'm trying: #include <vector> #include <iostream> #include <boost/foreach.hpp> #include <boost/range/adaptor/transformed.hpp> std::vector<int> f() { return std::vector<int>{1, 2, 3, 4}; } int add1(int x) { return x + 1; } int main() { for (int i : f() | boost::adaptors::transformed(add1)) std::cout << i << ' '; std::cout << '\n'; } The output is 1 3 4 5, instead of the 2 3 4 5 that I would expect. Is there something funny going on with temporaries? Does the temporary returned by f() not stick around for the duration of the loop? Thanks, Nate.