在std :: function和lambdas中使用auto说明符

时间:2017-07-25 09:40:28

标签: c++ lambda auto

我有一个名为Handler的类,它存储了一些lambda。我想要做的是拥有一个std::vector std::function来存储我的所有事件,例如。我真的无法弄清楚为什么lambda不像我预期的那样工作。

这是handler.h

class Handler
{
public:
    Handler();
    ~Handler();

    void Register(const char* outcome, std::function<auto()> lambda);
    void Trigger(const char* outcome);

private:
    std::vector<int> identifier;
    std::vector<char*> outcome;
    std::vector<std::function<auto()>> func;
};

handler.cpp

Handler::Handler()
{
    //ctor stuff here
}


Handler::~Handler()
{
    this->func.clear();
    this->outcome.clear();
    this->identifier.clear();
    //...
}

void Handler::Register(const char* outcome, std::function<auto()> lambda)
{
    static int identifier = 0;
    identifier++;
    this->outcome.push_back((char*)outcome);
    this->identifier.push_back(identifier);
    this->func.push_back(lambda);
    //Sort outcome
}

void Handler::Trigger(const char * outcome)
{
    int i;
    for (i = 0; i < this->identifier.size(); i++)
    {
        if (!strcmp(outcome, this->outcome.at(i)))
            break;
    }
    this->func[i]();
}

但是,如果我在Handler::Register中指定lambdas,它就不会让我抛出no suitable user-defined conversion from "lambda []void ()->void" to "std::function<auto()> exists。在此示例中,我使用void返回类型,但其他类型也有错误,我不明白为什么std::function的模板不能推断它,如果它正在发生的事情。

Handler* events = new Handler();

    events->Register("Up", [=]() -> void { //Error here!
        //do stuff
        //return something?
    });

有没有其他方法可以做到这一点,比如没有超载Handler::Register

1 个答案:

答案 0 :(得分:2)

auto不是类型,因此std::function<auto()>也不是类型。根据您的使用方式,std::function<void()>可能就是您想要的。

您的代码还存在其他问题,如评论中所述,因此我会将Handler更改为此

class Handler
{
public:
    Handler();
    // default ~Handler is fine

    void Register(std::string outcome, std::function<void()> lambda);
    void Trigger(const std::string & outcome outcome) const;
    void Trigger(std::size_t index) const;

private:
    using Outcomes = std::map<std::string, std::function<void()>/*, custom string comparator ?*/>;
    std::vector<Outcomes::iterator> identifier;
    Outcomes outcomes;
};

void Handler::Register(std::string outcome, std::function<void()> func)
{
    auto emplaced = outcomes.emplace(std::move(outcome), std::move(func));
    identifier.push_back(emplaced.first);
}

void Handler::Trigger(const std::string & outcome) const
{
    outcomes.at(outcome)();
}

void Handler::Trigger(std::size_t index) const
{
    identifier[index]->second();
}
相关问题