This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/49734] New: Adding using std::for_each inconsistent with return value
- From: "marat.buharov at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 13 Jul 2011 13:39:00 +0000
- Subject: [Bug c++/49734] New: Adding using std::for_each inconsistent with return value
- Auto-submitted: auto-generated
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: unassigned@gcc.gnu.org
ReportedBy: marat.buharov@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)