带有 unique_ptr 的工厂模式

迈克尔 S。

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

问题是,当我有:

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

这部分没问题,但在地图中我不能将 lambda 用于:

return std::make_unique<Predator>();

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

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

其余函数无法编译。我肯定在附近,我错过了一些关于模板的重要内容,但不知道是什么。你能给我一些提示吗?

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

代理工厂.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;
};

代理工厂.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
雷米勒博

creatorFunctionType被定义为

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

creatorFunctionType是一个指向函数的指针,它期望返回 a 的函数std::unique_ptr<Agent>

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

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

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

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>(); }
    );
}

使用上面的代码,lambdas 现在将返回std::unique_ptr<Agent>,满足creatorFunctionType预期。并且这些return语句仍然按原样工作,因为只要派生自std::unique_ptr<T>就可以用 a 初始化,这在您的情况下是正确的,因为派生自std::unique_ptr<U>UTPredatorPreyAgent

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章