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

By , last updated December 13, 2017

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;
}

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*