|
Boost Users : |
Subject: [Boost-users] [range] filtered dereferences underlying range elements twice
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-05-31 01:23:31
Hello,
It seems that the 'filtered' adaptor dereferences elements of the
underlying range twice if they pass the filter.
This is illustrated in the following example, where a range is
first transformed and then filtered:
#include <iostream>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm/copy.hpp>
int transform(int i)
{
std::cout << "calling transform() on " << i << "\n";
return i + 1;
}
bool filter(int i)
{
return i % 2 == 0;
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
int b[8];
boost::copy(a | boost::adaptors::transformed(transform)
| boost::adaptors::filtered(filter),
b);
return 0;
}
The output is:
calling transform() on 1
calling transform() on 1
calling transform() on 2
calling transform() on 3
calling transform() on 3
calling transform() on 4
calling transform() on 5
calling transform() on 5
calling transform() on 6
calling transform() on 7
calling transform() on 7
calling transform() on 8
As you can see, the transform function is called twice for elements
that pass the filter.
Can this be avoided? In my real use case, the transformation
performs a database lookup, and it should not be done twice.
Thanks,
Nate.
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