TestCase2并且TestCase3可以正常编译。但是,TestCase1我收到以下错误:
E0312, Custom conversion from "lambda []void ()->void" to
"EventHandler" is not appropriate.
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?我想知道如何解决。
#include <functional>
#include <iostream>
class EventHandler
{
std::function<void()> _func;
public:
int id;
static int counter;
EventHandler() : id{ 0 } {}
EventHandler(const std::function<void()>& func) : _func{ func }
{
id = ++EventHandler::counter;
}
};
int EventHandler::counter = 0;
int main()
{
EventHandler TestCase1 = []() {};
EventHandler TestCase2([]() {});
EventHandler TestCase3 = static_cast<std::function<void()>>([]() {});
}
Run Code Online (Sandbox Code Playgroud)