Domanda

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;
};
È stato utile?

Soluzione

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

Altri suggerimenti

There isn't any advantage to using std::move for any POD type, and a pointer is a POD type. std::move allows you to move some data rather than copy it. For example, if you std::move one std:string into another, the pointer to the underlying storage is copied instead of the entire array being copied. But note that the pointer value is still being copied. Thus, if all you're working with is the pointer, std::move has no advantage - it doesn't matter if that pointer is null or not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top