Add of std::valarray
got different sizes with different operand orders.
Code is as the following:
#include <iostream>
#include <valarray>
using namespace std;
int main() {
std::valarray<float> v0{3.f, 0.f};
std::valarray<float> v1{0.f};
std::valarray<float> v2 = v0 + v1;
std::valarray<float> v3 = v1 + v0;
cout << v2.size() << endl; // 2
cout << v3.size() << endl; // 1
}
Compilers:
g++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
clang version 9.0.0-2~ubuntu18.04.2 (tags/RELEASE_900/final)
operator+()
does not perfom concatenation of two std::valarray<float>
objects,
std::valarray<float> v2 = v0 + v1;
Here, since v1
is of size 1, it will add the only value in v1
to both the elements in v0
, hence size stays 2.
std::valarray<float> v2 = v1 + v0;
But here, v1
is of size 1, it will add the first element 3.f to the only element in v1
and second value of v0
is ignored.
This is what usually happens, but nevertheless the behaviour of binary operations on two valarray
s are undefined.