Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c2664 in Visual Studio 2012 when using make_pair

I dig up an old project and wanted to compile it, but received several errors, a few of those being a c2664:

error C2664: 'std::make_pair' : cannot convert parameter 1 from 'CUser *' to 'CUser *&&'
error C2664: 'std::make_pair' : cannot convert parameter 1 from 'unsigned long' to ' unsigned long &&'

The relevant code parts are:

//typedef for the userdata map
typedef std::map<unsigned long, std::pair<CUser*,userstatus*>> UserDataMapType;
//...
Inc::incret CUserManager::AddUser(unsigned long ID, CUser* pUser, userstatus* pUserStatus)
{
    //...
    std::pair<UserDataMapType::iterator, bool> ret = m_mapUserData.insert(std::make_pair<unsigned long, std::pair<CUser*, userstatus*>>(ID, std::make_pair<CUser*, userstatus*>(pUser, pUserStatus)));
    //...
}

I tried to make the function parameters const, but that did not help.

It did compile just fine in VS2010.

Please help me find what causes this and how to solve it.

like image 759
Juarrow Avatar asked Dec 16 '12 14:12

Juarrow


1 Answers

make_pair() has been changed in VS2012 to support a new C++11 feature called move semantics and I suspect that explicitly specifying the types for make_pair() is getting in the way.

Remember that make_pair() does not need any template parameters to be explicitly specified. It deduces them from the type of each argument.

Try removing the explicit template arguments from both calls to make_pair() like so...

std::pair<UserDataMapType::iterator, bool> ret = m_mapUserData.insert(std::make_pair(ID, std::make_pair(pUser, pUserStatus)));

Explicitly providing them like this would have worked fine pre-VS2012 because of a new C++11 feature added called move semantics. You'll want to read up on that subject later since you have a shiny new compiler that supports it.

like image 123
Sean Cline Avatar answered Oct 18 '22 03:10

Sean Cline