如何使用数组创建标准::函数

How can I create std::function with array?

本文关键字:函数 标准 创建 何使用 数组      更新时间:2023-10-16

我正在尝试用整数做函数数组。

我最终使用了这段代码:std::function<void()> func[100];

但是当我尝试像这样写入数组时:

func[option] = draw("optionName", 255);

我收到此错误:

Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion

编辑:

现在我使用此代码将函数写入数组:

func[option] = std::bind(draw, "optionName", 255);

但现在它给了我这些错误:

Error   C2672   'std::invoke': no matching overloaded function found (compiling source file source.cpp) Project C:Program Files (x86)Microsoft Visual Studio 14.0VCincludetype_traits  1491    
Error   C2893   Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' (compiling source file source.cpp) Project C:Program Files (x86)Microsoft Visual Studio 14.0VCincludetype_traits  1491    

您的代码尝试调用函数并将结果分配给数组。

如果要为函数调用分配特定参数,std::bind将帮助您做到这一点。

函数模板bind为 f 生成转发调用包装器。调用此包装器等效于调用 f 并将其某些参数绑定到 args。