Strange behaviour of std::valarray

I've got the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <valarray>

using namespace std;

int main()
{
	valarray<int> a(2);
	a[0] = 2; a[1] = 1;
	a = a[0] * a;
	cout << a[0] << ' ' << a[1] << endl;
	cin.peek();
}

When I compile it with Borland, the output is 4 2 (which is quite obvious), but when it comes to g++, the output is 4 4. I would understand that if it was
a *= a[0];
where modifying a using the reference to a[0] causes undefined behaviour. But in this case a temporary valarray should be created, the items of which are copied into a, shouldn't it? There are sequence points before and after the calls to operator* and operator=, so it's not the problem, either. But it is still executing as if it was
1
2
a[0] = a[0] * a[0];
a[1] = a[0] * a[1];

What's the matter?
Last edited on
Topic archived. No new replies allowed.