http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49734

           Summary: Adding using std::for_each inconsistent with return
                    value
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: marat.buha...@gmail.com


Look at that code (summing on c using indices in l):

---
#include <iostream>
#include <algorithm>

struct Adder {
    int acc;
    int *cv;

    void operator()(int x) { acc += cv[x]; std::cout << acc << std::endl; }
};

int main()
{
    int c[] = {1, 1, 1, 1, 1};
    int l[] = {0, 1, 2, 3, 4};

    Adder add = {0, c};
    std::for_each(l, l + 5, add);
    std::cout << add.acc << std::endl;
}
---

in void Adder::operator()(int) acc puts in stdout. And I can see as the value
in accumulator(acc) increments over iterationั‹ in std::for_each.

But in the last line of int main() it turns out that add.acc contains 0
(Expected value is 5)

Reply via email to