Since my example is somewhat complex, I have put a sample here that demonstrates the error (Code will be inlined below as well): http://coliru.stacked-crooked.com/a/a426995302bda656
#include <functional>
#include <unordered_map>
#include <memory>
enum class DeliveryMethodType
{
POST_MASTER,
BUBBLE,
};
class IDeliveryMethod
{
};
class BubbleDelivery : public IDeliveryMethod
{
};
template<typename Key, typename Interface>
class GenericFactory
{
public:
using FactoryMethod = std::function<std::unique_ptr<Interface> ()>;
static Key const& Register(Key const& key, FactoryMethod creator)
{
s_factoryMethods.insert({key, creator});
return key;
}
static std::unique_ptr<Interface> Create(Key const& key)
{
std::unique_ptr<Interface> obj;
auto it = s_factoryMethods.find(key);
if (it != s_factoryMethods.end())
{
obj = it->second();
}
return obj;
}
private:
static std::unordered_map<Key, FactoryMethod> s_factoryMethods;
};
template<typename Key, typename Interface>
std::unordered_map<Key, typename GenericFactory<Key, Interface>::FactoryMethod>
GenericFactory<Key, Interface>::s_factoryMethods;
using DeliveryMethodFactory = GenericFactory<DeliveryMethodType, IDeliveryMethod>;
static auto key = DeliveryMethodFactory::Register(DeliveryMethodType::BUBBLE, []() {
return std::unique_ptr<IDeliveryMethod>(new BubbleDelivery);
});
int main()
{
}
My design goal here is to create a generic static factory class. Each translation unit will (at static initialization time) invoke the Register() method for a specific specialization of GenericFactory for the desired key and factory method types.
I'm getting the following compiler error, which I'm not sure how to resolve.
error: implicit instantiation of undefined template 'std::hash<DeliveryMethodType>'
I imagine perhaps that my template trickery is failing here and I'm not doing something right. Can anyone help identify the issue here? Thanks.
-std=c++14bash-3.2$ c++ -c -std=c++1y ./main.cpp bash-3.2$ c++ -v Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.1.0 Thread model: posix