나는 c'tor 매개 변수 (아래 코드)에 가변 템플릿을 사용하는 클래스 팩토리를 가지고 있습니다. 그러나 그것을 사용하려고하면 컴파일 오류가 발생합니다. 원래 매개 변수없이 썼을 때 잘 동작했습니다. 여기 variadic 템플릿 문제가있는 C++ 0x 클래스 팩터 리
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...);
}
};
오류입니다 : 여기
는 클래스 호출 할 때 (예)
// Tucked out of the way
RegisterClass<DataMap, PDColumnMap, int, void *> RC_CT_PD(0);
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*))'
내가 할 수있는 ' 왜 그것이 컴파일되지 않을 것이며 광범위한 검색 결과를 찾지 못했습니다. 아무도 내가 잘못하고있는 것을 말해 줄 수 있습니까? (컨텍스트에서 의미가있는 이상한 변수 이름을 제외하고)?
이template <class Derived>
static void Register(const KeyType &key, FactFunType fn)
{
FnList[key] = fn;
}
당신은이 기능에 Derived
를 사용하지 않지만, 아마 GenericFactory<...>.Register(...)
를 해결하기 위해 GCC의 시도를 어질러 :
이러한 클래스는 어떻게 유용합니까? 컴파일 된 경우 어떻게 사용되는지에 대한 간단한 예제를 제공 할 수 있습니까? –
파생 된 클래스를 인스턴스화해야하지만 런타임에 필요한 클래스 만 알면됩니다. 'Args'매개 변수는 클래스 생성자에 인수가있는 경우 사용됩니다. – user350096
'GenericFactory' 질문에 포함 된 코드에서 템플릿 기반 클래스조차도 아닙니다. 어떤 도움을 얻기 위해 고쳐야 할 것 같아요. – doublep