在C++中,如果我尝试这样做:
std::function<void(bool,void)>
Run Code Online (Sandbox Code Playgroud)
那么编译器会抛出错误.为什么是这样?在许多情况下它很有用.一个例子:
//g++ -std=c++17 prblm.cpp
#include <cstdio>
#include <functional>
template<class type_t>
class some_callback
{
public:
using callback_t = std::function<void(bool,type_t)>;
some_callback(callback_t _myfunc)
{
this->myfunc = _myfunc;
}
callback_t myfunc;
};
using callback_with_just_bool = some_callback<void>;
using callback_with_an_int_too = some_callback<int>;
int main()
{
auto my_callback_with_int = callback_with_an_int_too([](bool x, int y)
{
}); //OK
auto my_callback_just_bool = callback_with_just_bool([](bool x)
{
}); //Error
auto my_callback_just_bool = callback_with_just_bool([](bool x,void z)
{
}); //Error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果用户希望在其回调中可选地具有附加数据,则这允许非常干净的语法,但不是必须的.但是,编译器将拒绝尝试初始化对象的代码callback_with_just_bool
为什么会这样,是否有一个干净的方式?谢谢.
编辑:在现实世界的代码中,我尝试这样做的具体原因是在事件系统中.有提供给事件系统有关希望单个对象的数据有条件接收事件(例如,"如果你足够接近源,您会收到一个声音事件")所提供的数据,以及一个回调有关的事件(例如"X200 Y200的10khz噪音").大多数情况下,检查需求所需的数据将存在于提供给 …