I have this piece of code summing std::valarray<int>
's:
#include <iostream>
#include <valarray>
#include <vector>
int main()
{
std::vector<std::valarray<int>> vectorOfValarrays{{1, 1}, {2, 2}, {3, 3}};
std::valarray<int> sumOfValarrays(2);
for (const auto& i : vectorOfValarrays)
sumOfValarrays = sumOfValarrays + i;
std::cout << sumOfValarrays[0] << ' ' << sumOfValarrays[1];
}
Compiling with x86-64 gcc 12.2
using -O0
and -O1
, it prints the expect result:
6 6
But when compiling with -O2
and -O3
, it prints:
3 3
What could be the reason for this? Is my code undefined behaviour or is this a gcc bug?