valarray, addition of slices

Hi,

I ran into a strange compilation problem with valarray.
Below is a minimal example to reproduce the error.
I am using g++ version 4.6.1 (also tried 4.4.5).

When compiling (2) I get the following error

test.cpp: In function ‘int main()’:
test.cpp:9:33: error: no match for ‘operator+’ in ‘std::valarray<_Tp>::operator[](std::slice) [with _Tp = int](std::slice(1u, 3u, 1u)) + std::valarray<_Tp>::operator[](std::slice) [with _Tp = int](std::slice(1u, 3u, 1u))’


It seems to me it is performing things in the wrong order; first doing addition before slicing.
When using a temporary variable this is not possible and so it compiles.

Does anybody have experience with this or is there a logical reason why I cannot use such a construction? Is this a bug??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <valarray>
using namespace std;

int main() {
  valarray<int> a(6),b(3);
  // (1) compiles 
  b+=a[slice(1,3,1)]; 
  // (2) does not compile 
  a[slice(0,3,1)]+a[slice(1,3,1)];

  /*****************************/
  // (3) compiles
  (valarray<int>)(a[slice(1,3,1)])+(valarray<int>)(a[slice(1,3,1)]); 
  // (4) does not compile 
  a[slice(1,3,1)]+(valarray<int>)(a[slice(1,3,1)]); 
  // (5) does not compile 
  (valarray<int>)(a[slice(1,3,1)])+a[slice(1,3,1)]; 
}


Thanks,
Jaap
Last edited on
The non-const version of valarray<T>::operator[] taking a slice returns a slice_array which doesn't have operator + but only operator +=. That why line 9, 15 and 17 doesn't compile.
Topic archived. No new replies allowed.