Should one use a std::move on a nullptr assignment?

bjackfly picture bjackfly · Aug 6, 2013 · Viewed 8.9k times · Source

I came across the following. Is there any advantage to doing a move on the nullptr? I assume it is basically assigning a zero to Node* so I am not sure if there is any advantage to do a move here. Any thoughts?

template <typename T>
struct Node
{
  Node(const T& t): data(t), next(std::move(nullptr)) { }
  Node(T&& t): data(std::move(t)), next(std::move(nullptr)) { }

  T data;
  Node* next;
};

Answer

Casey picture Casey · Aug 6, 2013

nullptr is by definition an rvalue (C++11 §2.14.7p1), so std::move(nullptr) is nullptr. It has no effect, just as would be the case for passing any other rvalue literal to std::move, e.g., std::move(3) or std::move(true).