|
Boost Users : |
Subject: [Boost-users] Reverse for loop with boost::adaptors::reverse crashes
From: Filip Konvièka (filip.konvicka_at_[hidden])
Date: 2014-11-25 11:11:43
Hi,
I was trying to replace BOOST_REVERSE_FOREACH with the standard
range-based for loop.
#include <boost/range/adaptor/reversed.hpp>
#include <boost/foreach.hpp>
#include <vector>
#include <iostream>
std::vector<int> getv() {
return std::vector<int>{1,2,3};
}
int main(int argc, char* argv[]) {
// works fine:
BOOST_REVERSE_FOREACH(int i, getv())
std::cout << i << std::endl;
// crashes:
for(int i : boost::adaptors::reverse(getv()))
std::cout << i << std::endl;
return 0;
}
Maybe this is basic C++ I should understand; it looks like the temporary
vector returned by getv() gets destroyed in the 2nd loop too soon. I
was wondering whether the adaptor could warn me that I'm using it in a
wrong way (the code looks so nice and compiles cleanly...).
Thanks,
Filip
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net