小编use*_*096的帖子

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

我有一个类工厂,我正在为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, …
Run Code Online (Sandbox Code Playgroud)

c++ templates factory c++11

7
推荐指数
1
解决办法
1451
查看次数

标签 统计

c++ ×1

c++11 ×1

factory ×1

templates ×1