C2664: void doProcess(Data_ptr &&) : cannot convert argument 1 from Data_ptr to Data_ptr &&

By , last updated July 3, 2019

This error is from Visual Studio, and happens when you try to call a method with move semantics (&&) and the type doesn’t understand it has to be moved.

Consider this code:

#include <memory>

// Struct to hold our data
struct Data
{
	int id = 0;
};

// Typedef of our unique pointer
typedef std::unique_ptr<Data>	Data_ptr;

void doProcess(Data_ptr && data)
{
	// Some code
}

int main()
{
	Data_ptr data(new Data());

	doProcess(data);	// C2446

	return 0;
}

If we change the highlighted line to doProcess(std::move(data)); the program will build just fine.

int main()
{
	Data_ptr data(new Data());
	doProcess(data);	// C2446

	Data_ptr otherdata(new Data());
	doProcess(std::move(otherdata));

	return 0;
}