I am trying to implement an std::unordered_map with std::string as the key and std::unique_ptr as the value. However, when I try to compile, I get the error:
error C2338: The C++ Standard doesn't provide a hash for this type.
Looking around at different questions, I know that C++11 does indeed include a std::hash < std::string >, and I can see no reason why this error would be thrown. I've tried to implement my own hashing function, like the one seen here, but it is still throwing the same error. I have also tried using __declspec(dllexport)
and making the copy constructor and assignment operator for the containing class private, as it is suggested in some threads to make unique_ptr work, but to no avail.
Here is the code for the offending class:
#ifndef __TEXTURE_MAP_H__
#define __TEXTURE_MAP_H__
#include <unordered_map>
#include <vector>
#include <memory>
#include <string>
//__declspec for std::unique_ptr compat.
class /*__declspec(dllexport)*/ TextureMap : virtual public IconRegister
{
private:
uint32 _textureId;
std::unordered_map<const std::string, std::unique_ptr<AtlasTexture> > _registeredIcons;
std::unordered_map<const char*, AtlasTexture*> _uploadedIcons;
std::vector<AtlasTexture*> _animatedIcons;
public:
TextureMap();
~TextureMap();
uint32 getTextureId();
void loadTextureAtlas();
/* override */ IIcon& registerIcon(const char*);
void registerIcons();
private:
TextureMap(const TextureMap& other) { }
TextureMap& operator= (const TextureMap& other) { return *this; };
};
#endif
I cannot find any reason this should not be working, and I've tried pretty much every other solution I could find when I searched for the problem.
I am using MSVC 2012.
Any help is greatly appreciated. Thanks.
EDIT: Addition of the AtlasTexture class: header and implementation
EDIT: My implementation of the move and move assignment: here.
_registeredIcons
key shouldn't beconst
- just plain oldstd::string
(hash
is specialised forstring
notconst string
). Also,std::hash(const char*)
will hash the pointer's address, not the pointed-to text.... is that really what you want? – Tony Delroyunique_ptr<>
isn't a valid type to map to... per 23.2.5.10 "key_type and mapped_type are sometimes required to be CopyAssignable" -unique_ptr
is not CopyAssignable. You could useshared_ptr
. – Tony Delroystd::unordered_map<std::string, std::unique_ptr<int>>
seems to be usable in VS2013.. – Casey