When I divide a valarray by its first element, only the first element becomes 1 and others keep their original value.
#include <iostream>
#include <valarray>
using namespace std;
int main() {
    valarray<double> arr({5,10,15,20,25});
    arr=arr/arr[0]; // or arr/=arr[0];
    for(int value:arr)cout << value << ' ';
    return 0;
}
The actual output is:
1 10 15 20 25
The expected output is:
1 2 3 4 5
Why is the actual output not as expected?
I use g++(4.8.1) with -std=c++11
 
                        
The details of why this happens are due to implementation tricks used in
valarrayto improve performance. Both libstdc++ and libc++ use expression templates for the results ofvalarrayoperations, rather than performing the operations immediately. This is explicitly allowed by [valarray.syn] p3 in the C++ standard:What happens in your example is that
arr/arr[0]doesn't perform the division immediately, but instead it returns an object like_Expr<__divide, _Valarray, _Constant, valarray<double>, double>which has a reference toarrand a reference toarr[0]. When that object is assigned to anothervalarraythe division operation is performed and the result stored directly into the left-hand side of the assignment (this avoids creating a temporaryvalarrayto store the result and then copying it into the left-hand side).Because in your example the left-hand side is the same object,
arr, it means that the reference toarr[0]stored in the expression template refers to a different value once the first element inarrhas been updated with the result.In other words, the end result is something like this:
The first iteration of the for-loop will set
arr[0]toarr[0] / arr[0]i.e.arr[0] = 1, and then all subsequent iterations will setarr[i] = arr[i] / 1which means the values don't change.I'm considering making a change to the libstdc++ implementation so that the expression template will store a
doubledirectly instead of holding a reference. This would meanarr[i] / divexpr.rhswill always evaluatearr[i] / 5and not use the updated value ofarr[i].