0

I want to do something like this:

    int data[4] = {1,2,3,4};
    int otherData[4];
    otherData = std::move(data);

compiler says invalid array assignment, so how can I transfer values from data to otherData

Demon King
  • 21
  • 1
  • No, you cannot move one C style array into another. – wohlstad Jul 06 '22 at 15:52
  • 6
    C-array are not moveable/copyable. [`std::array`](https://en.cppreference.com/w/cpp/container/array) is a good alternative. – Jarod42 Jul 06 '22 at 15:52
  • Why don't you use `memcpy`? – Ryan Zhang Jul 06 '22 at 15:53
  • BTW, moving `int` would be same as a copy. – Jarod42 Jul 06 '22 at 15:55
  • 2
    If you only have a few elements in the array and it's a fixed size, use `std::array` as @Jarod42 suggested. It'll then _copy_ the elements in the array (even if you `std::move()`) since fundamental types can only be copied. If you have many elements in the array that you want to move around, using a `std::vector` would perhaps be beneficial. – Ted Lyngmo Jul 06 '22 at 15:56
  • @DemonKing See here [`std::move` doesn't actually move anything](https://stackoverflow.com/a/27026280/12002570) – Jason Jul 06 '22 at 16:01
  • `std::array data{ 1, 2, 3, 4 }; std::array otherData; otherData = data;` – Eljay Jul 06 '22 at 17:50

0 Answers0