[odb-users] Odd behaviour with odb::result<T> container

Sean Clarke sean.clarke at sec-consulting.co.uk
Fri Jun 12 17:24:21 EDT 2015


Hi folks,
    I have an odd issue whereby I see different behaviour if I pass
odb::result containers to STL functions....

In a small test example I query the DB and retrieve say 20 invoices, if I
iterate over the invoices (this is just dummy test code):

for(const auto& invoice : iresult) {
  invoice.id();
  std::cout << "nett amount = £" << std::setprecision(2) << std::fixed <<
invoice.nett_amount() << std::endl;
}

I see lots of output.... however if I then pass the same container to an
STL function accumulate:

double average = std::accumulate(std::begin(iresult), std::end(iresult),
0.0, [](const double sum, const Invoice& i) { return sum + i.nett_amount();
});

I get zero returned.

If I change the accumulate lambda to count it's iterations it never gets
called (its like iresult.begin() == iresult.end() ).

If I remove that initial dummy for loop iterating over the container then
the accumulate function works fine. Similarly, if I remove the accumulate
function and add another for loop it again works fine.

Any ideas to explain what I am seeing?

Regards
Sean Clarke


More information about the odb-users mailing list