switch(Info)
{
  case a: return new A;
  case b: return new B;
  ...
  default:...
}
struct package
{
    void * funcSet;
    void * func;
    size_t index;
    string sig;
};
 
template <typename ...> class TypeList {};
 
template <typename AbstractProduct ,typename IdentifierType = string> class FactoryImpl
{
public:
    template <typename... Arg> bool Register(const IdentifierType& id,const function<unique_ptr<AbstractProduct>(Arg...)>& creator)
    {
 
        static vector<function<unique_ptr<AbstractProduct>(Arg...)>> vf;#17
        typename AssocMap::const_iterator i =associations_.find(id);
        if(i!= associations_.end()) return false;
        vf.push_back(creator);
        return associations_.insert(typename AssocMap::value_type(id,package {&vf,&vf.back(),vf.size()-1,string(typeid(TypeList<Arg...>).name())})).second;
 
    }
 
    template <typename ... Arg >
    bool UnRegister(const IdentifierType& id)
    {
        typename AssocMap::const_iterator i =associations_.find(id);
        if(i != associations_.end())
        {
            assert(
                ((i->second).sig).compare(typeid(TypeList<Arg...>).name())==0
            );
            auto vf=static_cast<vector<function<unique_ptr<AbstractProduct>(Arg...)>>*>((i->second).funcSet);
            vf->erase(vf->begin()+(i->second).index);
        }
 
        return associations_.erase(id)==1;
    }
 
    template <typename... Arg> unique_ptr<AbstractProduct> Createobject(const IdentifierType& id,Arg&&... args)
    {
        typename AssocMap::const_iterator i =associations_.find(id);
 
        if(i != associations_.end())
        {
            assert(((i->second).sig).compare(typeid(TypeList<Arg...>).name())==0);
            auto funp=static_cast<function<unique_ptr<AbstractProduct>(Arg...)>* >((i->second).func);
            return (*funp)(std::forward<Arg>(args)...);
        }
        assert(false);
    }
 
 
private:
    typedef std::unordered_map<IdentifierType,package> AssocMap;
    AssocMap associations_;
 
};因为没有在类模板使用可变参数,所以实现起来很是费劲,我现在倒觉得可以使用类模板带可变参数实现。这样类模板实例化的类,虽然只支持注册和调用参数固定调用体,但具有编译期类型检查的好处,Register和UnRegister均很容易实现。用户需要时,根据需要实例化模板即可。
template <typename AbstractProduct ,typename IdentifierType ,typename... Arg  > class Factory
{
public:
    bool Register(const IdentifierType& id, std::function<unique_ptr<AbstractProduct> (Arg...)> creator)
    {
        return associations_.insert(typename AssocMap::value_type(id,creator)).second;
    }
    bool UnRegister(const IdentifierType& id)
    {
        return associations_.erase(id)==1;
    }
    unique_ptr<AbstractProduct> Createobject(const IdentifierType& id,Arg&&... args)
    {
        typename AssocMap::const_iterator i =associations_.find(id);
        if(i != associations_.end())
        {
            return (i->second)(std::forward<Arg>(args)...);
        }
        assert(false);
    }
private:
    typedef std::unordered_map<IdentifierType,std::function<unique_ptr<AbstractProduct> (Arg...)> > AssocMap;
    AssocMap associations_;
};
原文:http://blog.csdn.net/zjq2008wd/article/details/43794471