I'm experiencing an constness issue when attempting to insert a pair into a map. The compiler error is:
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2089) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(const std::pair<_Ty1,_Ty2> &)' being compiled
1> with
1> [
1> _Ty1=const Assets::AssetId,
1> _Ty2=std::shared_ptr<Assets::Material>
1> ]
1> c:\fusionengine\meshgl.cpp(85) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const Assets::AssetId,
1> _Ty2=std::shared_ptr<Assets::Material>
1> ]
The line causing the error is:
m_materials.insert( MaterialsMap::value_type(pMaterial->AssetId(), pMaterial) );
The m_materials map is declared as follows:
typedef std::map< Assets::AssetId, std::shared_ptr<Material> > MaterialsMap;
typedef std::pair< Assets::AssetId, std::shared_ptr<Material> > MtlPair;
MaterialsMap m_materials;
Error 1 error C2166: l-value specifies const object c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility 114
Can anyone explain how I resolve this issue?
This code compiles fine with GCC:
#include <map>
#include <memory>
using namespace std;
typedef int AssetId;
struct Material {
int _id;
Material(int id) : _id(id) {}
int AssetId() const { return _id; }
};
typedef std::map< AssetId, std::shared_ptr<Material> > MaterialsMap;
MaterialsMap m_materials;
int main() {
std::shared_ptr<Material> pMaterial(new Material(42));
m_materials.insert( MaterialsMap::value_type(pMaterial->AssetId(), pMaterial) );
}
Either your example is incomplete or wrong, or this is a bug in the implementation of the standard library of MSVC2012. It shall not call operator=
in the above code.