C ++ 0x class factory with variational pattern problem

I have a factory class where I use variational patterns for c'tor parameters (code below). However, when I try to use it, I get compilation errors; when I originally wrote it without parameters, it worked fine.

Here is the class:

template< class Base, typename KeyType, class... Args > class GenericFactory { public: GenericFactory(const GenericFactory&) = delete; GenericFactory &operator=(const GenericFactory&) = delete; typedef Base* (*FactFunType)(Args...); template <class Derived> static void Register(const KeyType &key, FactFunType fn) { FnList[key] = fn; } static Base* Create(const KeyType &key, Args... args) { auto iter = FnList.find(key); if (iter == FnList.end()) return 0; else return (iter->second)(args...); } static GenericFactory &Instance() { static GenericFactory gf; return gf; } private: GenericFactory() = default; typedef std::unordered_map<KeyType, FactFunType> FnMap; static FnMap FnList; }; template <class B, class D, typename KeyType, class... Args> class RegisterClass { public: RegisterClass(const KeyType &key) { GenericFactory<B, KeyType, Args...>::Instance().Register(key, FactFn); } static B *FactFn(Args... args) { return new D(args...); } }; 

Here is the error: when called (for example)

 // Tucked out of the way RegisterClass<DataMap, PDColumnMap, int, void *> RC_CT_PD(0); 

GCC 4.5.0 gives me:

 In constructor 'RegisterClass<B, D, KeyType, Args>::RegisterClass(const KeyType&) [with B = DataMap, D = PDColumnMap, KeyType = int, Args = {void*}]': no matching function for call to 'GenericFactory<DataMap, int, void*>::Register(const int&, DataMap* (&)(void*))' 

I do not understand why it will not compile, and after an extensive search on Google, I could not find the answer. Can someone tell me what I'm doing wrong (except for the weird variable name, which makes sense in context)?

+7
c ++ c ++ 11 templates factory
source share
1 answer

I think we can say here:

 template <class Derived> static void Register(const KeyType &key, FactFunType fn) { FnList[key] = fn; } 

You are not using Derived in this function, but you will probably ruin gcc's attempt to allow GenericFactory<...>.Register(...) . You can also change this to GenericFactory<...>::Register(...) .

+2
source share

All Articles