使用unique_ptr的工厂模式

时间:2019-05-29 18:27:45

标签: c++ unique-ptr factory-pattern

我有抽象类Agent,很少有派生类:捕食者,猎物等。我想建立一个工厂,这给了我unique_ptr,可以将其存储在基类的向量中。

问题是,当我有以下情况时:

using creatorFunctionType = std::unique_ptr<Agent>(*)();

这部分还可以,但是在地图中,我不能将lambda用于:

return std::make_unique<Predator>();

但是当我尝试使用模板时:

template <class T>
using creatorFunctionType = std::unique_ptr<T>(*)();

其余功能无法编译。我肯定在附近,我错过了一些有关模板的重要知识,但不知道该怎么做。你能给我一些提示吗?

发布完整代码,可能会有所帮助

AgentFactory.h

#include "Interfaces/Agent.h"
#include "Enums.h"
#include <map>
#include <memory>

class AgentFactory
{
public:
    template <class T>
    using creatorFunctionType = std::unique_ptr<T>(*)();

    AgentFactory();
    std::unique_ptr<Agent> createAgent(Enums::AgentType agentType);
private:
    void registerAgentType(Enums::AgentType agentType, creatorFunctionType 
    creatorFunction);

    std::map<Enums::AgentType, creatorFunctionType> factoryRegister;
};

AgentFactory.cpp

#include "AgentFactory.h"
#include "Predator.h"
#include "Prey.h"

AgentFactory::AgentFactory()
{
    registerAgentType(Enums::AgentType::Predator, []() { return         
    std::make_unique<Predator>(); });
    registerAgentType(Enums::AgentType::Prey, []() { return     
    std::make_unique<Prey>(); });
}

std::unique_ptr<Agent> AgentFactory::createAgent(Enums::AgentType 
agentType)
{
    if (auto it = factoryRegister.find(agentType); it != 
    factoryRegister.end()) {
        return it->second();
    }

    return nullptr;
}

void AgentFactory::registerAgentType(Enums::AgentType agentType, 
creatorFunctionType creatorFunction)
{
    factoryRegister.insert(std::pair<Enums::AgentType, 
    creatorFunctionType>(agentType, creatorFunction));
}

编译错误:

1>d:\predator-prey\predator-prey\agentfactory.h(15): error C2955: 'AgentFactory::creatorFunctionType': use of alias template requires template argument list
1>d:\predator-prey\predator-prey\agentfactory.h(10): note: see declaration of 'AgentFactory::creatorFunctionType'
1>d:\predator-prey\predator-prey\agentfactory.h(17): error C3203: 'creatorFunctionType': unspecialized alias template can't be used as a template argument for template parameter '_Ty', expected a real type
1>d:\predator-prey\predator-prey\agentfactory.cpp(14): error C2064: term does not evaluate to a function taking 0 arguments
1>d:\predator-prey\predator-prey\agentfactory.cpp(22): error C3203: 'creatorFunctionType': unspecialized alias template can't be used as a template argument for template parameter '_Ty2', expected a real type
1>d:\predator-prey\predator-prey\agentfactory.cpp(22): fatal error C1903: unable to recover from previous error(s); stopping compilation

1 个答案:

答案 0 :(得分:3)

creatorFunctionType定义为

using creatorFunctionType = std::unique_ptr<Agent>(*)();

creatorFunctionType是指向函数的指针,该函数需要返回std::unique_ptr<Agent>的函数。

但是,您的lambda没有明确的返回类型,因此编译器会根据其std::unique_ptr<Predator>语句将其返回类型分别推导为std::unique_ptr<Prey>return

在这种情况下,非捕获的lambda可隐式转换为函数指针,但这是您想要的,但是lambda不会返回std::unique_ptr<Agent>,因此无法将它们分配给{{1} }。类型根本不匹配。

您需要明确说明lambda的返回类型,以便它们与creatorFunctionType期望的正确签名匹配,例如:

creatorFunctionType

使用上面的代码,lambda现在将返回AgentFactory::AgentFactory() { registerAgentType(Enums::AgentType::Predator, []() -> std::unique_ptr<Agent> { return std::make_unique<Predator>(); } ); registerAgentType(Enums::AgentType::Prey, []() -> std::unique_ptr<Agent> { return std::make_unique<Prey>(); } ); } ,满足std::unique_ptr<Agent>的期望。而且creatorFunctionType语句仍然可以按原样工作,因为return可以用std::unique_ptr<T>进行初始化,只要std::unique_ptr<U>是从U派生的,这对于您因为TPredator源自Prey