3

I don't understand the reason why std::move() had to be created. Couldn't everything just be static_cast<T&&>() instead? The only reason that I find why std::move() might have been created is just to make code easier to read. So, instead of seeing static_cast, programmers could see std::move() and understand what it is doing. Other than that, I see no purpose. Can someone help me understand why they created std::move()?.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    *programmers could see std::move and understand what it is doing* Is that not a good enough reason? Readability is extremely important. – super Jun 23 '21 at 19:59
  • 2
    Related or duplicate Q/A's: [C++11 move(x) actually means static_cast(x)?](https://stackoverflow.com/q/20533960/5023438), [is there any difference between static cast to rvalue reference and std::move](https://stackoverflow.com/q/17142167/5023438), [std::move() is just casting?](https://stackoverflow.com/q/47374251/5023438) – alter_igel Jun 23 '21 at 20:07
  • 3
    It's actually a little more than `static_cast` - it's `static_cast::type&&>(t)` - and with that, `std::move(t)` is beginning to look pretty nice. – Ted Lyngmo Jun 23 '21 at 20:07
  • 1
    Just to complicate matters, be careful not to confuse [`std::move()`](https://en.cppreference.com/w/cpp/utility/move) in `` with [`std::move()`](https://en.cppreference.com/w/cpp/algorithm/move) in ``. – Remy Lebeau Jun 23 '21 at 20:30

2 Answers2

7

Yes, it's mostly about readability.

In addition to clearly expressing the intent, std::move doesn't require you to manually write the type.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
2

A quick googling on the proposal n1377

The static_cast<A&&> syntax is admittedly ugly. This is not necessarily a bad thing as you want to clearly call out when you are doing something as dangerous (and useful!) as moving from an lvalue. But the cast can become so ugly as to be unreadable when the type A is a long complicated identifier.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86