具有可变参数模板问题的C++ 0x类工厂

use*_*096 7 c++ templates factory c++11

我有一个类工厂,我正在为c'tor参数使用可变参数模板(下面的代码).但是,当我尝试使用它时,我得到编译错误; 当我最初没有参数编写它时,它工作正常.

这是班级:

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...);
   }
};
Run Code Online (Sandbox Code Playgroud)

这是错误:调用时(例如)

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

GCC 4.5.0给了我:

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*))'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它不会编译,经过广泛的谷歌搜索后我找不到答案.任何人都可以告诉我我做错了什么(除了奇怪的变量名,这在上下文中有意义)?

MSN*_*MSN 2

我认为它在这里呕吐:

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

您不在Derived此函数中使用,但它可能会扰乱 gcc 解析GenericFactory<...>.Register(...). 您可能还想将其更改为GenericFactory<...>::Register(...).