我正在研究一个类,它允许您将具有公共基类型的类型动态注册到键,然后根据键动态构造该类型的实例。这是它现在的样子:
template<class Key, class Base>
class TypeRegistry
{
private:
FunctionRegistry<Key, Base*> m_registry;
template<class Derived>
static Base* make()
{
return new Derived();
}
public:
template<class Derived>
void register_type(const Key& key)
{
m_registry.register_function(key, &make<Derived>);
}
Base* make_type(const Key& key) const
{
auto maker = m_registry.get_function(key);
if(maker) return maker();
else return nullptr;
}
};
该类FunctionRegistry
有这个接口:
template<class Key, class Ret, class... Args>
class FunctionRegistry
{
public:
typedef Ret (*function_type)(Args...);
//register a key and function pointer
void register_function(const Key& key, function_type func);
//get a function pointer, or nullptr if the key is not registered
function_type get_function(const Key& key) const;
};
现在,我的问题是关于使用可变参数模板扩展 TypeRegistry 以支持构造函数参数。我不知道我应该执行 TypeRegistry::make 函数。这是我所希望的:
template<class Key, class Base, class... ConstructorArgs>
class TypeRegistry
{
private:
FunctionRegistry<Key, Base*, ConstructorArgs...> m_registry;
template<class Derived, ???>
static Base* make(???)
{
return new Derived(???);
}
public:
template<class Derived>
void register_type(const Key& key)
{
m_registry.register_function(key, &make<Derived, ???>);
}
template<class... DeterminedArgs>
Base* make_type(const Key& key, DeterminedArgs&&... args) const
{
auto maker = m_registry.get_function(key);
if(maker) return maker(std::forward<DeterminedArgs>(args)...);
else return nullptr;
}
};
问题是,我不知道如何正确模板化 make() 函数。大概它必须ConstructorArgs...
作为它的参数,以便它可以在 中正确注册FunctionRegistry
,但是我如何确保所有参数都正确转发(右值与左值)到Derived
构造函数?