Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why create std::move and not use static_cast [duplicate]

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()?.


2 Answers

Yes, it's mostly about readability.

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

like image 87
HolyBlackCat Avatar answered Oct 31 '25 07:10

HolyBlackCat


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.

like image 35
Tony Tannous Avatar answered Oct 31 '25 06:10

Tony Tannous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!