[Modern C++ ] [Interview Problems] std::move doesn’t move anything. It’s a Scam.

JustIdeas
3 min readMay 18, 2024

--

Move semantics is one of the most talked-about topics in Modern C++ interviews. If you’ve been through the interview process as an experienced developer, you’ve probably encountered it. Introduced in C++11 along with rvalue references, move semantics changed the game for resource management in C++. Talking about move semantics without mentioning rvalue references is like discussing alcohol without acknowledging the hangover.

std::move doesn’t move anything. Its not what it looks like.

Well in this post, I’m going to assume you have some basic knowledge of move semantics or at least know a bit about rvalue references. So instead of rehashing the basics, I want to dive into the common misconceptions that many C++ developers have about move semantics (std::move to be specific). Let’s clear up these misunderstandings and understand this powerful feature in more depth.

Now, if you were to ask about `std::move()` and its function, chances are you’d hear something along these lines:

std::move is a utility function in the C++ Standard Library that enables the transfer of resources from one object to another, specifically supporting move semantics.

Alright, let’s clear the air on this one. It’s like that tricky riddle you half-solve but never quite get to the bottom of. You know the type — neither completely true nor completely false. But if you leave it there, you’re bound to end up with some wonky assumptions about how things really work.

So, what’s the deal with `std::move()`?

Let’s kick things off by debunking a few myths, shall we?

  • First off, it doesn’t actually move anything.
  • Nope, it won’t generate executable code either.
  • And here’s the kicker: it’s like the ultimate couch potato — it does absolutely nothing at runtime.

Read those three points again and let them sink in. This might just blow your mind (or at least it did for me). Alright, now that we’ve cleared up what std::move() doesn't do, let's dive into what it actually is.

std::move is actually a function template and it perform cast. Yes, all it does is CAST. std::move unconditionally casts its arguments to an rvalue. That’s it. Thats the complete story.

Here is a simple implementation of std::move in C++11 to make the story more believable -

template<typename T>
typename remove_reference<T>::type&& move(T&& param)
{
using ReturnType = typename remove_reference<T>::type&&;

return static_cast<ReturnType>(param);
}

As, you can see std::move function takes a universal reference to an object and it returns a reference to the same object. Thats all. No Gimmick, just the truth. The truth will set you free. And with that I hope now, you know what exactly is going under the hood of std::move().

Bonus Info:- std::forward() doesn't facilitate forwarding per se. Rather, it performs conditional casting, distinguishing itself from the straightforward casting mechanism of std::move().

--

--

JustIdeas

https://topmate.io/cppelite -- Engineer by day, jokester by night. I'm always looking for ways to make the world a better place, one laugh at a time!