1

I am working on some projects and have a problem. I have a class that is too big and don't want to copy objects and want to use a smarter solution. So, if I don't want to copy, I can move() it. But the problem is that I am not using C++11 but C++10 (as the people in my company said)

I can't use the std::move() function and have to implement it in my source code.

Can somebody help me to do that in efficient way?

  • 3
    If your compiler doesn't support `std::move`, then it also doesn't support the concept of moving at all - no move constructors, no rvalue references. You could provide a `swap` method and/or specialize `std::swap` for your class, so it could be swapped efficiently with an empty instance. The using code would have to call `swap` explicitly, there's no syntactic sugar for it. – Igor Tandetnik Feb 26 '20 at 16:39
  • maybe making a `move()` method for your class(es)? – max66 Feb 26 '20 at 16:39
  • 3
    C++10? Wasn't the last standard before C++11 called C++03? – Fred Larson Feb 26 '20 at 16:43
  • @IgorTandetnik, I would use the move() function inside a method() so I can't dereference a pointer because this = &foo; is illegal :/ I have to move the object without using pointers. Is there some solution with shared pointers? –  Feb 26 '20 at 16:43
  • 2
    `std::move()` is only **one way** to avoid copying an object. Copies have been avoided long before C++11. Your cleanest solution is probably to write code that doesn't need _move_. – Drew Dormann Feb 26 '20 at 16:44
  • @max66, how can I do that? PS: I can't overload an operator=. –  Feb 26 '20 at 16:45
  • Like I said, you could write `this->swap(foo);`, assuming you provide an efficient `swap` implementation (if you can't provide an efficient `swap` implementation, then you couldn't implement an efficient move constructor in C++11 either). – Igor Tandetnik Feb 26 '20 at 16:46
  • No, before C++11 you don't have rvalue references so you can't write two different (copy and move) assignment operators. I mean: instead of `operator=()`, you could use two methods: `copy()` and `move()` – max66 Feb 26 '20 at 16:47
  • `C++10 (as the people in my company said)` looks like your coworkers like stupid jokes. – Marek R Feb 26 '20 at 16:56
  • 2
    Does this answer your question? [Is it possible to define a 'move-and-swap idiom' equivalent in C++03](https://stackoverflow.com/questions/60296661/is-it-possible-to-define-a-move-and-swap-idiom-equivalent-in-c03) – phuclv Feb 26 '20 at 16:57
  • related: [How were move semantics addressed before C++11?](https://stackoverflow.com/q/41031592/995714), [C++03 moving a vector into a class member through constructor (move semantics)](https://stackoverflow.com/q/25507858/995714) – phuclv Feb 26 '20 at 17:00
  • Actually, I want to avoid swap() because it is not efficient. I'm copying the whole Object and then the original version will be replaced with the value and in the end, the value will be replaced with the copy of the original object. It is more efficient to copy all the member variables inside operator= definition. –  Feb 28 '20 at 11:03

0 Answers0