STL algorithms and GiNaC::lst

Sheplyakov Alexei varg at thsun1.jinr.ru
Sun Jan 25 11:19:51 CET 2004


Hi!

Here is a simple program:


#include <iostream>
#include <algorithm>
#include <functional>
#include <stdexcept>
using namespace std;
#include <ginac/ginac.h>
using namespace GiNaC;


class lst_prod {
	ex prod;
	public:
	lst_prod() : prod(ex(1)) { }
	lst_prod(const ex & init_) : prod(init_) { }
	void operator()(const ex & x) {
		cout << "We are called with argument " << x << endl;
		prod *= x;
		cout << "Current value of product is " << prod << endl;
	}
	ex result() const {
		return prod;
	}
};

int main() {
	symbol a("a"), b("b"), c("c");
	lst l;
	l = a, b, c;
	lst_prod test;
	for_each(l.begin(), l.end(), test);
	ex res = test.result();
	cout << "---------------------";
	cout << "List is " << l << endl;
	cout << "Product of it's elements is " << res << endl;
	return 0;
}

I expected res to be a*b*c, but the output is:

We are called with argument a
Current value of product is a
We are called with argument b
Current value of product is b*a
We are called with argument c
Current value of product is c*b*a
---------------------
List is {a,b,c}
Product of it's elements is 1

This looks like a bug or a very strange feature...

-------------- next part --------------
#include <iostream>
#include <algorithm>
#include <functional>
#include <stdexcept>
using namespace std;
#include <ginac/ginac.h>
using namespace GiNaC;


class lst_prod {
	ex prod;
	public:
	lst_prod() : prod(ex(1)) { }
	lst_prod(const ex & init_) : prod(init_) { }
	void operator()(const ex & x) {
		cout << "We are called with argument " << x << endl;
		prod *= x;
		cout << "Current value of product is " << prod << endl;
	}
	ex result() const {
		return prod;
	}
};

int main() {
	symbol a("a"), b("b"), c("c");
	lst l;
	l = a, b, c;
	lst_prod test;
	for_each(l.begin(), l.end(), test);
	ex res = test.result();
	cout << "---------------------";
	cout << "List is " << l << endl;
	cout << "Product of it's elements is " << res << endl;
	return 0;
}

//  vim:ts=2:sw=2



More information about the GiNaC-list mailing list