带有模板的模板参数初始化列表

时间:2019-10-01 13:54:16

标签: c++ templates

我正在尝试对课程进行模板化

template <typename OperationT, typename OperationCommandT>
struct OperationLink {
    OperationT operationT;
    OperationCommandT operationCommandT;
    bool isEnabled;
    // Param param;
};
我要在映射类中使用的

template <typename OperationLinkT>
class OperationList
{

public:
    OperationList(){};
    std::unordered_map<Operation, OperationLinkT> operationListTable
    {
        {Operation::NOOP, {std::shared_ptr<Noop> noop, std::shared_ptr<NoopCommand> noopCommand, true}},
        {Operation::ERRORCHECK, {std::shared_ptr<Errorcheck> errorcheck, std::shared_ptr<ErrorcheckCommand> errorcheckCommand, true}},
        {Operation::BASEINFO, {std::shared_ptr<Baseinfo> baseinfo, std::shared_ptr<BaseinfoCommand> baseinfoCommand, true}},

但是我在初始化列表构造上犯了一些错误...我得到error: expected '(' for function-style cast or type construction 有人知道我在做什么错吗?

编辑:对,忘记添加:

// Operation: do nothing
class Noop : public Workflow
{

public:
    Noop(std::shared_ptr<Context> context) : Workflow(context) {};

    bool init();
    bool execute();
};

// Operation: print a banner, together with get the c++ program info
class Baseinfo : public Workflow
{
public:
    Baseinfo(std::shared_ptr<Context> context) : Workflow(context) {};

    bool init();
    bool execute();
};

要模板化的类...当然,它们都具有相同的结构

1 个答案:

答案 0 :(得分:1)

{std::shared_ptr<Noop> noop, std::shared_ptr<NoopCommand> noopCommand, true}

对于构造函数而言不是有效的初始化列表:您正在指定左值类型,但您正在传递这些对象,而不是为函数声明参数。你想要

{std::make_shared<Noop>(), std::make_shared<NoopCommand>(), true}
相关问题