我有一个名为 Handler
的类至极存储一些lambdas。我想做的是有一个 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
?
最佳答案
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();
}
关于c++ - 在 std::function 和 lambda 中使用自动说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299488/