3
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?

cigien
  • 57,834
  • 11
  • 73
  • 112
issac
  • 155
  • 8

1 Answers1

2

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 }
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Thank you for your explanation. But if I used GNU C++, I got different results, after s1=s2, I got s1: 0, 1, 2 instead of 0 0 2. I tried clang++ 3.7 and 9.0, I also got 0, 1, 2. so I'm confused with different results. Is there a standard that says in this case, 0,0,2 is correct or 0,1,2 is correct? Thanks! – issac Sep 10 '20 at 23:01
  • please note that I downloaded the clang from [link](https://releases.llvm.org/download.html). thanks. – issac Sep 10 '20 at 23:07
  • 1
    @issac Remove the `const` from the declaration of `s1` and you'll get `0 0 2`. I'm not actually sure why it compiles with `const` at all. – cigien Sep 11 '20 at 00:50
  • Thank you very much for pointing it out, removing const, I got consistent result. Thanks! – issac Sep 11 '20 at 14:37