Is it correct to assign a slice_array to another slice_array?

173 views Asked by At
int input[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::valarray<int> test(input, sizeof(input)/sizeof(input[0]));

const std::slice_array<int> s1 = test[std::slice(1, 3, 2)];
const std::slice_array<int> s2 = test[std::slice(0, 3, 1)];

// is it correct to do this?
s1 = s2;

std::valarray<int> temp(s1);
printf("temp[0]=%d  temp[1]=%d temp[2]=%d\n", temp[0], temp[1], temp[2]);

Running the code, I got:

test: 0 1 2 3 4 5 6 7 8 9 

s1:     1   3   5

s2:   0 1 2 

s1=s2

s1:     0   0   2     --> 0 1 2 is expected for s1

test: 0 0 2 0 4 5 6 7 8 9 

I'm just wondering if s1 = s2 is correct to use?

If it is correct to use, then is it safe to say that it is a bug for my old version of LLVM C++ library?

1

There are 1 answers

4
cigien On BEST ANSWER

Yes, you can assign one std::slice_array to another with operator=

Assigns the selected elements from sl_arr to the referred to elements of *this.

Also, there's no bug here, the result of s1 = [0, 0, 2] is correct.

In your case:

test { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
s1        ^     ^     ^
s2     ^  ^  ^

Notice that the 1st element referred to by s2 is 0 which gets assigned to the 1st element of s1, which is the 2nd element of test.

This newly assigned value then is the 2nd value of s2 which gets assigned to the 2nd value of s1, and so on.

At the end of the assignment, test becomes

test { 0, 0, 2, 0, 4, 2, 6, 7, 8, 9 }