
On 12.03.2011, at 11:12, Nathan Ridge wrote:
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
#include 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?
That's very likely the reason. You could test that by making your own container wrapper that writes messages on construction and destruction. Sebastian